Update A30.java

This commit is contained in:
Mrhongxing committed 2026-10-19 00:36:03 +08:00
1 parent 6a0ea68a2f
commit 872aee5d20
1 file changed
+27 -46
+27 -46
View File
@@ -15,59 +15,40 @@ public class A30 {
* } * }
*/ */
public List<Integer> findSubstring(String s, String[] words) { public List<Integer> findSubstring(String s, String[] words) {
/* int left=0;
* int time=0; int right=1;
* ArrayList<String> temp = new ArrayList<>(); int w=words[0].length();
* aaa(time,words,temp); char[] chars=s.toCharArray();
* HashMap<String,Integer> a=new HashMap<>();
* List<Integer> result = new ArrayList<>(); HashMap<String,Integer> temp=new HashMap<>();
* for(String x:words){
* temp.setLength(0);
* }
*/
char[] a = s.toCharArray();
HashMap<String, Integer> temp = new HashMap<>();
List<Integer> result = new ArrayList<>();
HashSet<String> temp1 = new HashSet<>();
for(String x:words){ for(String x:words){
a.put(x, a.getOrDefault(x, 0)+1);
temp.put(x, 0); temp.put(x, 0);
} }
int left=0; int count=a.size();
int right=words[0].toCharArray().length+left; while(right+w<=s.length()){
StringBuilder sb = new StringBuilder(); String sub = s.substring(right, right+w);
int time=0; if(temp.containsKey(sub)){
int time1=0; temp.put(sub, temp.get(sub)+1);
while (right<a.length) { if(temp.get(sub)>a.get(sub)){
for(String x:words){ while(temp.get(sub)>a.get(sub)){
if(s.substring(left,right).equals(sb.toString()+x)&&!temp1.contains(x)){ String sub1 = s.substring(left, left+w);
sb.append(x); if(temp.containsKey(sub1)){
temp1.add(x); temp.put(sub1, temp.get(sub1)-1);
time++;
}else if(s.subSequence(right-words[0].toCharArray().length, right).equals(x)&&temp1.contains(x)){ if(temp.get(sub1)==a.get(sub1)){
sb.setLength(0);
temp1.clear(); }
sb.append(x); }
temp1.add(x); left+=w;
time=1; }
left=right-words[0].toCharArray().length; }else if(temp.get(sub)==a.get(sub)){
count--;
} }
} }
if(time==words.length){
temp1.remove(s.substring(left,left+words[0].toCharArray().length));
result.add(left);
time--;
left+=3;
sb.delete(0,words[0].toCharArray().length);
}
if(time==0){ right+=w;
left++;
}
right++;
} }
//s.substring(left,right);
return result;
} }
public static void main(String[] args) { public static void main(String[] args) {
A30 solution = new A30(); A30 solution = new A30();