Given many words
, words[i]
has weight i
.
Design a class WordFilter
that supports one function, WordFilter.f(String prefix, String suffix)
. It will return the word with given prefix
and suffix
with maximum weight. If no word exists, return -1.
Examples:
Input: WordFilter(["apple"]) WordFilter.f("a", "e") // returns 0 WordFilter.f("b", "") // returns -1
Note:
words
has length in range[1, 15000]
.- For each test case, up to
words.length
queriesWordFilter.f
may be made. words[i]
has length in range[1, 10]
.prefix, suffix
have lengths in range[0, 10]
.words[i]
andprefix, suffix
queries consist of lowercase letters only.
给定多个 words
,words[i]
的权重为 i
。
设计一个类 WordFilter
实现函数WordFilter.f(String prefix, String suffix)
。这个函数将返回具有前缀 prefix
和后缀suffix
的词的最大权重。如果没有这样的词,返回 -1。
例子:
输入: WordFilter(["apple"]) WordFilter.f("a", "e") // 返回 0 WordFilter.f("b", "") // 返回 -1
注意:
words
的长度在[1, 15000]
之间。- 对于每个测试用例,最多会有
words.length
次对WordFilter.f
的调用。 words[i]
的长度在[1, 10]
之间。prefix, suffix
的长度在[0, 10]
之前。words[i]
和prefix, suffix
只包含小写字母。
playground测试通过
1 class WordFilter { 2 var input:[String] = [String]() 3 4 init(_ words: [String]) { 5 input = words 6 } 7 8 func f(_ prefix: String, _ suffix: String) -> Int { 9 for (index,str) in input.enumerated() 10 { 11 if str.hasPrefix(prefix) && str.hasSuffix(suffix) 12 { 13 return index 14 } 15 } 16 return -1 17 } 18 } 19 20 /** 21 * Your WordFilter object will be instantiated and called as such: 22 * let obj = WordFilter(words) 23 * let ret_1: Int = obj.f(prefix, suffix) 24 */ 25
playground测试通过
1 class WordFilter { 2 var mp:[String:[Int]] = [String:[Int]]() 3 var ms:[String:[Int]] = [String:[Int]]() 4 5 init(_ words: [String]) { 6 for k in 0..<words.count 7 { 8 for i in 0...words[k].count 9 { 10 mp[words[k].subString(0, i),default:[Int]()].append(k) 11 } 12 for i in 0...words[k].count 13 { 14 ms[words[k].subString(words[k].count - i),default:[Int]()].append(k) 15 } 16 } 17 18 } 19 20 func f(_ prefix: String, _ suffix: String) -> Int { 21 if mp[prefix] == nil || ms[suffix] == nil {return -1} 22 var pre:[Int] = mp[prefix]! 23 var suf:[Int] = ms[suffix]! 24 var i:Int = pre.count - 1 25 var j:Int = suf.count - 1 26 while (i >= 0 && j >= 0) 27 { 28 if pre[i] < suf[j] 29 { 30 j -= 1 31 } 32 else if pre[i] > suf[j] 33 { 34 i -= 1 35 } 36 else 37 { 38 return pre[i] 39 } 40 } 41 return -1 42 } 43 } 44 45 /** 46 * Your WordFilter object will be instantiated and called as such: 47 * let obj = WordFilter(words) 48 * let ret_1: Int = obj.f(prefix, suffix) 49 */ 50 51 extension String { 52 // 截取字符串:指定索引和字符数 53 // - begin: 开始截取处索引 54 // - count: 截取的字符数量 55 func subString(_ begin:Int,_ count:Int) -> String { 56 let start = self.index(self.startIndex, offsetBy: max(0, begin)) 57 let end = self.index(self.startIndex, offsetBy: min(self.count, begin + count)) 58 return String(self[start..<end]) 59 } 60 61 // 截取字符串:从index到结束处 62 // - Parameter index: 开始索引 63 // - Returns: 子字符串 64 func subString(_ index: Int) -> String { 65 let theIndex = self.index(self.endIndex, offsetBy: index - self.count) 66 return String(self[theIndex..<endIndex]) 67 } 68 }