Created
May 16, 2013 04:04
-
-
Save mogutou1214/5589307 to your computer and use it in GitHub Desktop.
Careercup1.2 - Given two strings, write a method to decide if one is a permutation of the other.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* test.cpp | |
Given two strings, write a method to decide if one is a permutation of the other. | |
* Created on: 2013-5-5 | |
* Author: Fred | |
*/ | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
bool chk_permutation(string str1, string str2){ | |
if(str1.length()!=str2.length()) return false; | |
int arr1[128]; | |
int arr2[128]; | |
for(int i=0;i<128;i++){ | |
arr1[i]=0; | |
arr2[i]=0; | |
} | |
for(unsigned int i=0;i<str1.length();i++){ | |
int ASCII_value = (int)str1[i]; | |
arr1[ASCII_value]++; | |
} | |
for(unsigned int j=0;j<str2.length();j++){ | |
int ASCII_value = (int)str2[j]; | |
arr2[ASCII_value]++; | |
} | |
for(int i=0;i<128;i++){ | |
if(arr1[i]!=arr2[i]) return false; | |
} | |
return true; | |
} | |
int main(){ | |
char str1[] = "hello"; | |
char str2[] = "lloeh"; | |
cout << chk_permutation(str1,str2)<<"\n"; | |
char str3[] = "hello12131"; | |
char str4[] = "lloehqwqd1"; | |
cout << chk_permutation(str3,str4)<<"\n"; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment