题解 | #牛牛的串联子串游戏#
牛牛的串联子串游戏
https://www.nowcoder.com/practice/c1984371372b43f3b10bf6d0231520bb
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @param words string字符串一维数组 * @return int整型一维数组 */ public int[] findSubstring (String s, String[] words) { // write code here List<Integer> indexL = new ArrayList<>(); int subStringLength = words.length * words[0].length(); for (int i=0; i <= (s.length() - subStringLength); i++){ String pStr = s.substring(i, i+subStringLength); if (checkIfValidStr(pStr, words)){ indexL.add(i); } } int[] results = indexL.stream().mapToInt(Integer::valueOf).toArray(); return results; } public static boolean checkIfValidStr(String s, String[] words){ for (String w : words){ s = s.replaceFirst(w, ""); } if (s.length() == 0){ return true; } return false; } }