lintcode:字符串置换

题目

给定两个字符串,请设计一个方法来判定其中一个字符串是否为另一个字符串的置换。

置换的意思是,通过改变顺序可以使得两个字符串相等。

样例

"abc" 为 "cba" 的置换。

"aabc" 不是 "abcc" 的置换。

解题

定义一个数组统计字符次数,去除字符次数,数组是否为空

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
        if(A==null && B==null)
            return true;
        if(A==null || B==null)
            return false;
        if(A.length() !=B.length())
            return false;
        int[] C = new int[256];
        
        for(int i=0;i<A.length();i++){
            char c1 = A.charAt(i);
            char c2 = B.charAt(i);
            C[c1]++;
            C[c2]--;
        }
        for(int i=0;i<256;i++){
            if(C[i]!=0)
                return false;
        }
        return true;
    }
}
原文地址:https://www.cnblogs.com/theskulls/p/5625010.html