description:
Given two strings, write a method to decide if one is a permutation of the other.
Have you met this question in a real interview? Yes
Example
abcd is a permutation of bcad, but abbe is not a permutation of abe
解题思路:
开始,使用的是将string转变为char[]的方式进行处理的,代码量非常大,但是,这道题目标志的是easy,而数组的是medium,后来看到了这种使用char的ascii的方式来进行处理的,简直是神奇,太赞了!
public class Solution {
/**
* @param A a string
* @param B a string
* @return a boolean
*/
public boolean stringPermutation(String A, String B) {
// Write your code here
int[] arr = new int[500];
for (int i = 0; i < A.length(); i++) {
arr[(int)A.charAt(i)]++;
}
for (int i = 0; i < B.length(); i++) {
arr[(int)B.charAt(i)]--;
}
for (int i = 0; i < 500; i++) {
if (arr[i] != 0) {
return false;
}
}
return true;
}
}