Update A30.java
This commit is contained in:
1 parent
872aee5d20
commit
c5025637ee
1 file changed
+31
-13
+31
-13
@@ -16,9 +16,10 @@ public class A30 {
|
||||
*/
|
||||
public List<Integer> findSubstring(String s, String[] words) {
|
||||
int left = 0;
|
||||
int right=1;
|
||||
int w = words[0].length();
|
||||
char[] chars=s.toCharArray();
|
||||
int right = 0;
|
||||
int is = 0;
|
||||
ArrayList<Integer> finalArray = new ArrayList<>();
|
||||
HashMap<String, Integer> a = new HashMap<>();
|
||||
HashMap<String, Integer> temp = new HashMap<>();
|
||||
for (String x : words) {
|
||||
@@ -29,31 +30,48 @@ public class A30 {
|
||||
while (right + w <= s.length()) {
|
||||
String sub = s.substring(right, right + w);
|
||||
if (temp.containsKey(sub)) {
|
||||
is = 1;
|
||||
temp.put(sub, temp.get(sub) + 1);
|
||||
if (temp.get(sub) > a.get(sub)) {
|
||||
while (temp.get(sub) > a.get(sub)) {
|
||||
String sub1 = s.substring(left, left + w);
|
||||
if(temp.containsKey(sub1)){
|
||||
temp.put(sub1, temp.get(sub1)-1);
|
||||
|
||||
if(temp.get(sub1)==a.get(sub1)){
|
||||
|
||||
}
|
||||
}
|
||||
left += w;
|
||||
if (temp.containsKey(sub1)) {
|
||||
if (temp.get(sub1) == a.get(sub1)) {
|
||||
count++;
|
||||
}
|
||||
temp.put(sub1, temp.get(sub1) - 1);
|
||||
}
|
||||
}
|
||||
} else if (temp.get(sub) == a.get(sub)) {
|
||||
count--;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if (count == 0) {
|
||||
finalArray.add(left);
|
||||
}
|
||||
} else {
|
||||
is = 0;
|
||||
count = a.size();
|
||||
left = right + w;
|
||||
for (String x : words) {
|
||||
temp.put(x, 0);
|
||||
}
|
||||
}
|
||||
if (is == 0) {
|
||||
right++;
|
||||
left = right;
|
||||
} else {
|
||||
right += w;
|
||||
}
|
||||
}
|
||||
return finalArray;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
A30 solution = new A30();
|
||||
String s = "barfoofoobarthefoobarman";
|
||||
String[] words = { "bar", "foo", "the" };
|
||||
String s = "aaaaaaaaaaaaaa";
|
||||
String[] words = { "aa", "aa" };
|
||||
System.out.println(solution.findSubstring(s, words));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user