Advertisement
GokulDeep

SubStringfromArray

Oct 18th, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. public static List<Integer> findSubstring(String s, String[] words) {
  2. List<Integer> ans = new ArrayList<>();
  3. if (s.length() == 0 || words.length == 0 || (words.length * words[0].length() > s.length())) {
  4. return ans;
  5. }
  6. Map<String, Integer> wordFreq = new HashMap<>();
  7. for (String w : words) {
  8. wordFreq.put(w, wordFreq.getOrDefault(w, 0) + 1);
  9. }
  10. int n = words.length;
  11. int l = words[0].length();
  12. Map<String, Integer> tmp = new HashMap<>();
  13. for (int i = 0; i <= s.length() - (n * l); i++) {
  14. for (int j = i; j < i + (n * l); j += l) {
  15. tmp.put(s.substring(j, j + l), tmp.getOrDefault(s.substring(j, j + l), 0) + 1);
  16. }
  17. int flag = 1;
  18. for (int j = 0; j < n; j++) {
  19. if (!tmp.containsKey(words[j]) || !wordFreq.get(words[j]).equals(tmp.get(words[j]))) {
  20. flag = 0;
  21. break;
  22. }
  23. }
  24. if (flag == 1) {
  25. ans.add(i);
  26. }
  27. tmp.clear();
  28. }
  29. return ans;
  30. }
  31.  
  32.  
  33. Testcase 181 failed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement