Description
Given-strings, write a method to decide if one was a permutation of the other.
Example
abcdis a permutation bcad of, but are not abbe a permutation ofabe
Problem solving: A similar problem has been encountered, the simpler way is to convert the string into a character array, and then sort, compare the number of each bit is equal. This is less efficient and the code is as follows:
1 Public classSolution {2 /**3 * @parama:a String4 * @paramb:a String5 * @return: A Boolean6 */7 Public Booleanpermutation (string A, String B) {8 //Write your code here9 if(A.length ()! =b.length ())Ten return false; One Char[]a =A.tochararray (); A Char[]B =B.tochararray (); - Arrays.sort (a); - Arrays.sort (b); the for(inti = 0; i < a.length; i++){ - if(A[i]! =B[i]) - return false; - } + return true; - } +}
It can also be done through a hash table: if the number of each element is equal, and the total number is the same, the string is exactly equal, refer to: 51212982.
211. String permutation "Lintcode by Java"