publicstaticvoidmain(String[] args){ Scanner input = new Scanner(System.in); while (input.hasNext()) { String aim = input.next(); String str = input.next(); int m = aim.length(); int i = 0; int j = m - 1; char[] charAim = aim.toCharArray(); Arrays.sort(charAim); int flag = 0; while (j != m) { char[] sub = str.substring(i, j + 1).toCharArray(); Arrays.sort(sub); flag = 0; for (int k = 0; k < sub.length; k++) { if (sub[k] != charAim[k]) { flag = 1; break; } } if (flag == 1) { i++; j++; continue; } else { System.out.println("true"); break; } } if (flag == 1) System.out.println("false"); } }
publicintcontainExactly(String a, String b){ if (a == null || b == null || a.length() < b.length()) { return -1; } char[] str = a.toCharArray(); char[] aim = b.toCharArray(); int[] count = newint[256]; for (int i = 0; i < aim.length; i++) { count[aim[i]]++; } int M = aim.length; int inValidTimes = 0; int R = 0; // 先让窗口用于M个字符 for (; R < M; R++) { if (count[str[R]]-- <= 0) { inValidTimes++; } } // 如果第一个是同源异构词 if (inValidTimes == 0) { return R - M; } // 窗口滑动 for (; R < str.length; R++) { if (inValidTimes == 0) { return R - M; } // 0[0..M-1]M // [1..M] // [2...M+1] if (count[str[R]]-- <= 0) { inValidTimes++; } if (count[str[R - M]]++ < 0) { inValidTimes--; } } return inValidTimes == 0 ? R - M : -1; }