[Swift]LeetCode745. 前缀和后缀搜索 | Prefix and Suffix Search

Given many wordswords[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:

  1. words has length in range [1, 15000].
  2. For each test case, up to words.length queries WordFilter.fmay be made.
  3. words[i] has length in range [1, 10].
  4. prefix, suffix have lengths in range [0, 10].
  5. words[i] and prefix, suffixqueries consist of lowercase letters only.

给定多个 wordswords[i] 的权重为 i 。

设计一个类 WordFilter 实现函数WordFilter.f(String prefix, String suffix)。这个函数将返回具有前缀 prefix 和后缀suffix 的词的最大权重。如果没有这样的词,返回 -1。

例子:

输入:
WordFilter(["apple"])
WordFilter.f("a", "e") // 返回 0
WordFilter.f("b", "") // 返回 -1

注意:

  1. words的长度在[1, 15000]之间。
  2. 对于每个测试用例,最多会有words.length次对WordFilter.f的调用。
  3. words[i]的长度在[1, 10]之间。
  4. prefix, suffix的长度在[0, 10]之前。
  5. 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 }

猜你喜欢

转载自www.cnblogs.com/strengthen/p/10525441.html