Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static List<Integer> findSubstring(String s, String[] words) {
- List<Integer> ans = new ArrayList<>();
- if (s.length() == 0 || words.length == 0 || (words.length * words[0].length() > s.length())) {
- return ans;
- }
- Map<String, Integer> wordFreq = new HashMap<>();
- for (String w : words) {
- wordFreq.put(w, wordFreq.getOrDefault(w, 0) + 1);
- }
- int n = words.length;
- int l = words[0].length();
- Map<String, Integer> tmp = new HashMap<>();
- for (int i = 0; i <= s.length() - (n * l); i++) {
- for (int j = i; j < i + (n * l); j += l) {
- tmp.put(s.substring(j, j + l), tmp.getOrDefault(s.substring(j, j + l), 0) + 1);
- }
- int flag = 1;
- for (int j = 0; j < n; j++) {
- if (!tmp.containsKey(words[j]) || !wordFreq.get(words[j]).equals(tmp.get(words[j]))) {
- flag = 0;
- break;
- }
- }
- if (flag == 1) {
- ans.add(i);
- }
- tmp.clear();
- }
- return ans;
- }
- Testcase 181 failed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement