# Check Permutation

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

``````#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

// O(nlogn) + O(n) algorithm
bool isPermutation(string one, string two) {
sort(one.begin(), one.end());
sort(two.begin(), two.end());
if (one == two)
return true;
return false;
}

int main()
{
cout << boolalpha << isPermutation("god", "dog") << endl;
cout << boolalpha << isPermutation("abcdefg", "gfedcba") << endl;
}``````
``````#include <iostream>
#include <string>

using namespace std;

// Assumed ASCII
// O(3n) algorithm
// O(1) space
bool isPermutation(string one, string two) {
if (one.length() != two.length())
return false;

int checker[256] = { 0 };
// initialize all to zero

for (int i = 0; i < one.size(); i++) {
int val = one.at(i);
++checker[val];
}

for (int i = 0; i < two.size(); i++) {
int val = two.at(i);
--checker[val];
}

for (int i = 0; i < sizeof(checker) / sizeof(int); i++) {
if (checker[i] != 0) return false;
}
return true;
}

int main()
{
cout << boolalpha << isPermutation("god", "dog") << endl;