public class Samestring { //thought two: Each character corresponds to its own ASC code, the first idea of the algorithm complexity of O (Nlogn), can generally use space to reduce the complexity of time//can open a size of 256 array space, and 256 array elements are set to 0, and then traverse the first string to the character's ASC as an array subscript, the array elements are added 1,//and then traverse the second string to reduce the value of the array element 1, if the last array element value is 0, the description is the same string. The time complexity of this algorithm is O (n) public static void compare (String s1,string s2) {byte[] b1=s1.getbytes (); byte[] B2=s2.getbytes (); int[] Bcount=new int[256];for (int i=0;i<256;i++) {bcount[i]=0;} for (int i=0;i<b1.length;i++) {bcount[b1[i]-' 0 ']++;} for (int i=0;i<b2.length;i++) {bcount[b2[i]-' 0 ']--;} for (int i=0;i<256;i++) if (bcount[i]!=0) {System.out.println ("not equal!"); return;} System.out.println ("equal!");} public static void Main (string args[]) {string s1= "AAABBBCCC"; String s2= "ABABACBCC"; compare (s1, S2); String s3= "AABBCCDD"; String s4= "abc"; compare (S3, S4);}}
Results: Equalnot Equal
The String3 of the Java Programmer's written interview