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

abcdis a permutation ofbcad, butabbeis not a permutation ofabe

class Solution {
public:
    /*
     * @param A: a string
     * @param B: a string
     * @return: a boolean
     */
    bool Permutation(string A, string B) {
        // write your code here
        if (A.length() != B.length()) {
            return false;
        }
        int cnts[256] = {0};
        for (char c : A) {
            ++cnts[c];
        }
        for (char c : B) {
            --cnts[c];
            if (cnts[c] < 0) {
                return false;
            }
        }
        return true;
    }
};

results matching ""

    No results matching ""