Update A30.java

This commit is contained in:
Mrhongxing committed 2026-10-19 02:18:40 +08:00
1 parent 872aee5d20
commit c5025637ee
1 file changed
+47 -29
+47 -29
View File
@@ -15,45 +15,63 @@ public class A30 {
* } * }
*/ */
public List<Integer> findSubstring(String s, String[] words) { public List<Integer> findSubstring(String s, String[] words) {
int left=0; int left = 0;
int right=1; int w = words[0].length();
int w=words[0].length(); int right = 0;
char[] chars=s.toCharArray(); int is = 0;
HashMap<String,Integer> a=new HashMap<>(); ArrayList<Integer> finalArray = new ArrayList<>();
HashMap<String,Integer> temp=new HashMap<>(); HashMap<String, Integer> a = new HashMap<>();
for(String x:words){ HashMap<String, Integer> temp = new HashMap<>();
a.put(x, a.getOrDefault(x, 0)+1); for (String x : words) {
a.put(x, a.getOrDefault(x, 0) + 1);
temp.put(x, 0); temp.put(x, 0);
} }
int count=a.size(); int count = a.size();
while(right+w<=s.length()){ while (right + w <= s.length()) {
String sub = s.substring(right, right+w); String sub = s.substring(right, right + w);
if(temp.containsKey(sub)){ if (temp.containsKey(sub)) {
temp.put(sub, temp.get(sub)+1); is = 1;
if(temp.get(sub)>a.get(sub)){ temp.put(sub, temp.get(sub) + 1);
while(temp.get(sub)>a.get(sub)){ if (temp.get(sub) > a.get(sub)) {
String sub1 = s.substring(left, left+w); while (temp.get(sub) > a.get(sub)) {
if(temp.containsKey(sub1)){ String sub1 = s.substring(left, left + w);
temp.put(sub1, temp.get(sub1)-1); left += w;
if (temp.containsKey(sub1)) {
if(temp.get(sub1)==a.get(sub1)){ if (temp.get(sub1) == a.get(sub1)) {
count++;
}
temp.put(sub1, temp.get(sub1) - 1);
} }
} }
left+=w; } else if (temp.get(sub) == a.get(sub)) {
}
}else if(temp.get(sub)==a.get(sub)){
count--; 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;
} }
right+=w;
}
}
public static void main(String[] args) { public static void main(String[] args) {
A30 solution = new A30(); A30 solution = new A30();
String s = "barfoofoobarthefoobarman"; String s = "aaaaaaaaaaaaaa";
String[] words = { "bar", "foo", "the" }; String[] words = { "aa", "aa" };
System.out.println(solution.findSubstring(s, words)); System.out.println(solution.findSubstring(s, words));
} }
} }