CTCI: Ch. 1-3

Given two strings, write a method to decide if one is a permutation of the other.

#include <iostream>
#include <string.h>
using namespace std;
bool isPermutationStr(string s1, string s2);

bool isPermutationStr(string s1, string s2)
{
    if(s1.length() != s2.length())
        return false;
    else
    {
        sort(s1.begin(),s1.end());
        sort(s2.begin(),s2.end());
        if( s1 == s2)
            return true;
        else
            return false;
    }
}

int main(int argc, const char * argv[])
{
    string s1 = "abcde";
    string s2 = "eadcb";
    if(isPermutationStr(s1,s2))
        cout<<"TRUE";
    else
        cout<<"FALSE";
    
    return 0;
}

 

Leave a Reply