Skip to content

Instantly share code, notes, and snippets.

@frank4565
Last active August 29, 2015 13:56
Show Gist options
  • Save frank4565/8934296 to your computer and use it in GitHub Desktop.
Save frank4565/8934296 to your computer and use it in GitHub Desktop.
1.3 Given two strings, write a method to decide if one is a permutation of the other.
#include<iostream>
using namespace std;
unsigned const NUM_CHAR = 256;
bool isPermutation1(string str1, string str2)
{
sort(str1.begin(), str1.end());
sort(str2.begin(), str2.end());
return str1 == str2;
}
bool isPermutation2(string str1, string str2)
{
if (str1.size() != str2.size()) return false;
int a[NUM_CHAR]{};
for (char c : str1) a[c]++;
for (char c : str2) {
if (--a[c] < 0) return false;
}
return true;
}
int main(int argc, char *argv[])
{
cout << isPermutation1("31", "13") << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment