-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSubtringWithConcatenationOfAllWords.java
80 lines (63 loc) · 2.59 KB
/
SubtringWithConcatenationOfAllWords.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by abc on 01/01/2016.
*/
public class SubtringWithConcatenationOfAllWords {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> result = new ArrayList<>(); //holds the result
//add each word in the map to maintain the counter
Map<String , Integer> holder = new HashMap<>();
//maintained the other map if any case same words are repeated twice
//or thrice like abc abc bef
Map<String , Integer> matcher = new HashMap<>();
for (String key:
words) {
if(holder.containsKey(key)){
int value = holder.get(key);
holder.put(key,++value);
}else {
holder.put(key, 1);
}
matcher.put(key,0);
}
//holds the pointer to start the matching
int total = s.length();
int wordLength = words[0].length();
int allWordsSize = (words.length * wordLength);
System.err.println("allWordsSizeallWordsSize "+allWordsSize+" BHA "+total);
for(int i=0; i< total;i++){
//next matching will be unnecessary because the remaining
//string cant hold all the matches
if((total - i) < allWordsSize) {
System.err.println("------------------------------ "+i);
break;
}
int curr = i;
for(int j=0;j<words.length;j++){
String eachWord = s.substring(curr,(curr + wordLength));
// System.out.println("Word "+eachWord +" curr "+curr+" wo "+wordLength);
if(matcher.containsKey(eachWord)){
int value = matcher.get(eachWord);
matcher.put(eachWord,++value);
}else{
//if the key is not present it means the sequence broke
//just stop
break;
}
curr += wordLength;
}
// System.out.println("EVER EQUAL "+(matcher.equals(holder))+" matchermatcher "+matcher.values()+" holder "+holder.values()+" i "+i);
if(matcher.equals(holder)){
result.add(i);
}
matcher.replaceAll((k,v) -> 0);
}
return result;
}
public static void main(String[] args) {
System.out.println(new SubtringWithConcatenationOfAllWords().findSubstring("lingmindraboofooowingdingbarrwingmonkeypoundcake",new String[]{"fooo","barr","wing","ding","wing"}));
}
}