import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @param words string字符串一维数组 * @return int整型一维数组 */ public int[] findSubstring (String s, String[] words) { // write code here int n = s.length(); if (s.isEmpty() || words.length == 0) { return new int[0]; } HashMap<String, Integer> map = new HashMap<>(); for (String word : words) { map.put(word, map.getOrDefault(word, 0) + 1); } List<Integer> list = new ArrayList<>(); int wordLen = words[0].length(); int subStrLen = words.length * words[0].length(); for (int i = 0; i <= n - subStrLen; i++) { HashMap<String, Integer> windowMap = new HashMap<>(map); int j = i; while (j < n) { String word = s.substring(j, j + wordLen); if (windowMap.containsKey(word)) { int cnt = windowMap.get(word); if (cnt == 1) { windowMap.remove(word); } else { windowMap.put(word, cnt - 1); } j += wordLen; } else { break; } } if (windowMap.isEmpty()) { list.add(i); } } int[] res = new int[list.size()]; for (int i = 0; i < list.size(); i++) { res[i] = list.get(i); } return res; } }