Anagrams: A string that reverses the alphabetical order
This paper provides three methods to analyze the complexity of time and space respectively.
Method One : Violent Traversal complexity of Time: O (n^2)
method Two: Based on the sorting algorithm , the time complexity of the sorting is O (N*log (n)). So first the two-character numbers are sorted and then judged.
1 PublicclassCustomstringutil2 { 3 4BooleanFirstisanagram (String sfirst, string ssecond)5 { 6Char[] Cfirstarray =Sfirst.tochararray (); 7Char[] Csecondarray =Sfirst.tochararray (); 8 9 Arrays.sort (Cfirstarray); Ten Arrays.sort (Csecondarray); One Areturnarrays.equals (Cfirstarray, Csecondarray); - } -}
Analysis:
(1) convert a String to char[], time:O (n), Space: O (n)(array occupied)
(2) sort the array: time:O (N*log (n)), Space:o (n)
(2) Compare these two arrays: time:o (n), Space:O (1)(some temporary counters may use a bit of space)
Summary: The time complexity of this algorithm is: O (N*log (n))
Method Three: The idea of applying a hash table.
first we know that in each string in an ASCII Table corresponds to an integer number, and here we use this as an array subscript so that a character corresponds to the only position in the array.
Algorithmic Thinking : We can use an array, an array subscript is the index of the character, and then count the number of occurrences of each character in the string. In the first string, each occurrence of a character is added to the corresponding array position, and in the second string, each occurrence of a character is reduced by one in the corresponding position of the array. We do this by walking through the first string, and then traversing the second string, the only case where the anagrams is the last array or the whole 0. This means that a specific number of characters in the two strings are the same.
Algorithm steps:
(1) generating an integer array of 236 bits k
(2) for Each character x in the first string Sfirst,the integer value corresponding to X is y, put k[y] plus 1
(3) for each character x in the second string Ssecond,x corresponds to the integer value y, which K[y] minus 1
(4) If the array K is still all zeros, then the string sfirst and Ssecond are anagrams
Code :
1 Public classCustomstringutil2 {3 Public Static BooleanSecondisanagram (String sfirst, string ssecond)4 {5 if(Sfirst.length ()! =ssecond.length ())6 {7 return false;8 }9 int[] Asciichars =New int[256];Ten for(inti = Sfirst.length ()-1; i>=0; --i) One { A++asciichars[sfirst.charat (i)];//Key Code - } - for(inti = Sfirst.length ()-1; i>=0; --i) the { - CharCurrchar =Ssecond.charat (i); - if(Asciichars[currchar] = = 0) - { + return false; - } +--Asciichars[currchar]; A } at return true; - } -}
Analysis: Time: O (n)(time to traverse an array) space O (1)(space does not vary with the size of the processed string)
Note: The above code makes a hypothesis: we have a set of zero characters. Note that this hypothesis is a huge pitfall for programming, and we should be very careful.
How to tell if a two string is Anagrams_java implemented