Substring with Concatenation of All Words

You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.
For example, given:
S: "barfoothefoobarman"
L: ["foo", "bar"]

You should return the indices: [0,9].
(order does not matter).

LeetCode: Substring with Concatenation of All Words 解题报告

SOLUTION 1:

1. 使用HashMap来保存L中所有的字串。

2. 暴力破解之。使用i记录我们的查找结果字符串的位置,j记录单个单词的查找位置。j每次移动一个L中单词的位置。

3. 注意各种越界条件:i查到离结束还有L*N(L中所有单词总长)的时候,即需要停止。

    j 也要考虑每一次查找的单词的长度。

4. 使用第二个HashMap来记录我们查到的单词。如果所有的单词都查到了,即可记录一个解。

 1 // SOLUTION 1:
 2     public List<Integer> findSubstring1(String S, String[] L) {
 3         HashMap<String, Integer> map = new HashMap<String, Integer>();
 4         HashMap<String, Integer> found = new HashMap<String, Integer>();
 5         List<Integer> ret = new ArrayList<Integer>();
 6         
 7         if (S == null || L == null || L.length == 0) {
 8             return ret;
 9         }
10         
11         int cntL = 0;
12         
13         // put all the strings into the map.
14         for (String s: L) {
15             if (map.containsKey(s)) {
16                 map.put(s, map.get(s) + 1);
17             } else {
18                 map.put(s, 1);
19                 cntL++;
20             }
21         }
22         
23         int lenL = L[0].length();
24         
25         int cntFound = 0;
26         
27         // 注意这里的条件:i < S.length() - lenL * L.length
28         // 这里很关键,如果长度不够了,不需要再继续查找 
29         for (int i = 0; i <= S.length() - lenL * L.length; i++) {
30             // clear the found hashmap.
31             found.clear();
32             cntFound = 0;
33             
34             // 一次前进一个L的length.
35             // 注意j <= S.length() - lenL; 防止越界
36             for (int j = i; j <= S.length() - lenL; j += lenL) {
37                 String sub = S.substring(j, j + lenL);
38                 if (map.containsKey(sub)) {
39                     if (found.containsKey(sub)) {
40                         if (found.get(sub) == map.get(sub)) {
41                             // 超过了限制数目
42                             break;
43                         }
44                         
45                         found.put(sub, found.get(sub) + 1);
46                     } else {
47                         found.put(sub, 1);
48                     }
49                     
50                     if (found.get(sub) == map.get(sub)) {
51                         cntFound++;
52                     }
53                     
54                     // L中所有的字符串都已经找到了。
55                     if (cntFound == cntL) {
56                         ret.add(i);
57                     }
58                 } else {
59                     // 不符合条件,可以break,i前进到下一个匹配位置
60                     break;
61                 }
62             }
63         }
64         
65         return ret;
66     }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-14
  • 2022-02-26
  • 2021-10-17
猜你喜欢
  • 2021-06-24
  • 2021-06-01
  • 2022-01-12
  • 2021-05-30
  • 2022-02-02
相关资源
相似解决方案