-
Notifications
You must be signed in to change notification settings - Fork 1
/
SubstringWithConcatenationOfAllWords.java
55 lines (48 loc) · 1.58 KB
/
SubstringWithConcatenationOfAllWords.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.smlnskgmail.jaman.leetcodejava.hard;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// https://leetcode.com/problems/substring-with-concatenation-of-all-words/
public class SubstringWithConcatenationOfAllWords {
private final Map<String, Integer> wordCount = new HashMap<>();
private int wordLength;
private int substringSize;
private int k;
private final String s;
private final String[] words;
public SubstringWithConcatenationOfAllWords(String s, String[] words) {
this.s = s;
this.words = words;
}
public List<Integer> solution() {
int n = s.length();
k = words.length;
wordLength = words[0].length();
substringSize = wordLength * k;
for (String word : words) {
wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
}
List<Integer> result = new ArrayList<>();
for (int i = 0; i < n - substringSize + 1; i++) {
if (check(i, s)) {
result.add(i);
}
}
return result;
}
private boolean check(int i, String s) {
Map<String, Integer> rem = new HashMap<>(wordCount);
int wordsUsed = 0;
for (int j = i; j < i + substringSize; j += wordLength) {
String sub = s.substring(j, j + wordLength);
if (rem.getOrDefault(sub, 0) != 0) {
rem.put(sub, rem.get(sub) - 1);
wordsUsed++;
} else {
break;
}
}
return wordsUsed == k;
}
}