diff --git a/src/com/hong/www/A30.java b/src/com/hong/www/A30.java index 2775106..e707291 100644 --- a/src/com/hong/www/A30.java +++ b/src/com/hong/www/A30.java @@ -15,45 +15,63 @@ public class A30 { * } */ public List findSubstring(String s, String[] words) { - int left=0; - int right=1; - int w=words[0].length(); - char[] chars=s.toCharArray(); - HashMap a=new HashMap<>(); - HashMap 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 finalArray = new ArrayList<>(); + HashMap a = new HashMap<>(); + HashMap 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)); } }