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
+45 -27
+45 -27
View File
@@ -15,45 +15,63 @@ 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();
HashMap<String,Integer> a=new HashMap<>();
HashMap<String,Integer> temp=new HashMap<>();
for(String x:words){
a.put(x, a.getOrDefault(x, 0)+1);
int left = 0;
int w = words[0].length();
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) {
a.put(x, a.getOrDefault(x, 0) + 1);
temp.put(x, 0);
}
int count=a.size();
while(right+w<=s.length()){
String sub = s.substring(right, right+w);
if(temp.containsKey(sub)){
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)){
int count = a.size();
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);
left += w;
if (temp.containsKey(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--;
}
if (count == 0) {
finalArray.add(left);
}
} else {
is = 0;
count = a.size();
left = right + w;
for (String x : words) {
temp.put(x, 0);
}
}
right+=w;
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));
}
}