@@ -109,7 +109,56 @@ An O(N) solution with detailed explanation
109
109
110
110
111
111
``` java
112
+ public List<Integer > findSubstringByWindowSlide(String s, String [] words) {
113
+ List<Integer > resultList = new ArrayList<Integer > ();
114
+ if (s == null || s. length() == 0 || words == null || words. length == 0 ) {
115
+ return resultList;
116
+ }
117
+ Map<String , Integer > wordMap = new HashMap<String , Integer > ();
118
+
119
+ int distinguishCount = 0 ;
120
+ for (String word : words) {
121
+ wordMap. put(word, wordMap. getOrDefault(word, 0 ) + 1 );
122
+ if (wordMap. get(word) == 1 ) {
123
+ distinguishCount += 1 ;
124
+ }
125
+ }
126
+ int wordLen = words[0 ]. length();
127
+ int wordCount = words. length;
128
+ for (int k = 0 ; k < wordLen; k++ ) {
129
+ Map<String , Integer > foundMap = new HashMap<String , Integer > ();
130
+ int foundCount = 0 ;
131
+ for (int i = k, j = k; j <= s. length() - wordLen; j = j + wordLen) {
132
+
133
+ if (j - i >= wordLen * wordCount) {
134
+ String firstWord = s. substring(i, i + wordLen);
135
+ if (wordMap. containsKey(firstWord)) {
136
+ foundMap. put(firstWord, foundMap. get(firstWord) - 1 );
137
+ if (foundMap. get(firstWord) == wordMap. get(firstWord) - 1 ) {
138
+ foundCount -= 1 ;
139
+ }
140
+ }
112
141
142
+ i += wordLen;
143
+ }
144
+
145
+ String nextWord = s. substring(j, j + wordLen);
146
+ if (wordMap. containsKey(nextWord)) {
147
+ foundMap. put(nextWord, foundMap. getOrDefault(nextWord, 0 ) + 1 );
148
+ if (wordMap. get(nextWord) == foundMap. get(nextWord)) {
149
+ foundCount += 1 ;
150
+ }
151
+ }
152
+
153
+ if (foundCount == distinguishCount) {
154
+ resultList. add(i);
155
+ }
156
+ }
157
+
158
+ }
159
+
160
+ return resultList;
161
+ }
113
162
114
163
```
115
164
0 commit comments