You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
2.7 KiB
116 lines
2.7 KiB
package class001;
|
|
|
|
import java.util.Arrays;
|
|
|
|
public class Code02_ContainAllCharExactly {
|
|
|
|
public static int containExactly1(String s, String a) {
|
|
if (s == null || a == null || s.length() < a.length()) {
|
|
return -1;
|
|
}
|
|
char[] aim = a.toCharArray();
|
|
Arrays.sort(aim);
|
|
String aimSort = String.valueOf(aim);
|
|
for (int L = 0; L < s.length(); L++) {
|
|
for (int R = L; R < s.length(); R++) {
|
|
char[] cur = s.substring(L, R + 1).toCharArray();
|
|
Arrays.sort(cur);
|
|
String curSort = String.valueOf(cur);
|
|
if (curSort.equals(aimSort)) {
|
|
return L;
|
|
}
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public static int containExactly2(String s, String a) {
|
|
if (s == null || a == null || s.length() < a.length()) {
|
|
return -1;
|
|
}
|
|
char[] str = s.toCharArray();
|
|
char[] aim = a.toCharArray();
|
|
for (int L = 0; L <= str.length - aim.length; L++) {
|
|
if (isCountEqual(str, L, aim)) {
|
|
return L;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public static boolean isCountEqual(char[] str, int L, char[] aim) {
|
|
int[] count = new int[256];
|
|
for (int i = 0; i < aim.length; i++) {
|
|
count[aim[i]]++;
|
|
}
|
|
for (int i = 0; i < aim.length; i++) {
|
|
if (count[str[L + i]]-- == 0) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static int containExactly3(String s, String a) {
|
|
if (s == null || a == null || s.length() < a.length()) {
|
|
return -1;
|
|
}
|
|
char[] aim = a.toCharArray();
|
|
int[] count = new int[256];
|
|
for (int i = 0; i < aim.length; i++) {
|
|
count[aim[i]]++;
|
|
}
|
|
int M = aim.length;
|
|
char[] str = s.toCharArray();
|
|
int inValidTimes = 0;
|
|
int R = 0;
|
|
for (; R < M; R++) {
|
|
if (count[str[R]]-- <= 0) {
|
|
inValidTimes++;
|
|
}
|
|
}
|
|
for (; R < str.length; R++) {
|
|
if (inValidTimes == 0) {
|
|
return R - M;
|
|
}
|
|
if (count[str[R]]-- <= 0) {
|
|
inValidTimes++;
|
|
}
|
|
if (count[str[R - M]]++ < 0) {
|
|
inValidTimes--;
|
|
}
|
|
}
|
|
return inValidTimes == 0 ? R - M : -1;
|
|
}
|
|
|
|
// for test
|
|
public static String getRandomString(int possibilities, int maxSize) {
|
|
char[] ans = new char[(int) (Math.random() * maxSize) + 1];
|
|
for (int i = 0; i < ans.length; i++) {
|
|
ans[i] = (char) ((int) (Math.random() * possibilities) + 'a');
|
|
}
|
|
return String.valueOf(ans);
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
int possibilities = 5;
|
|
int strMaxSize = 20;
|
|
int aimMaxSize = 5;
|
|
int testTimes = 500000;
|
|
System.out.println("test begin, test time : " + testTimes);
|
|
for (int i = 0; i < testTimes; i++) {
|
|
String str = getRandomString(possibilities, strMaxSize);
|
|
String aim = getRandomString(possibilities, aimMaxSize);
|
|
int ans1 = containExactly1(str, aim);
|
|
int ans2 = containExactly2(str, aim);
|
|
int ans3 = containExactly3(str, aim);
|
|
if (ans1 != ans2 || ans2 != ans3) {
|
|
System.out.println("Oops!");
|
|
}
|
|
}
|
|
System.out.println("test finish");
|
|
|
|
}
|
|
|
|
}
|