is Permutation
***
Given two strings, write a method to decide if one is a permutation of the other.
***/
/***
1. if size is different, return false.
2. next_permutation, chk..
***/
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool isPermutation(string *str1, string *str2) {
if (str1->size() != str2->size()) return false;
do {
if(*str1 == *str2)
return true;
} while(next_permutation(str2->begin(), str2->end()));
return false;
}
Test
void main() {
string str1 = "happy";
string str2 = "ahppy";
if (isPermutation(&str1, &str2))
{
cout<<"yes"<<endl;
} else cout<<"no"<<endl;
}
Given two strings, write a method to decide if one is a permutation of the other.
***/
/***
1. if size is different, return false.
2. next_permutation, chk..
***/
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
bool isPermutation(string *str1, string *str2) {
if (str1->size() != str2->size()) return false;
do {
if(*str1 == *str2)
return true;
} while(next_permutation(str2->begin(), str2->end()));
return false;
}
Test
void main() {
string str1 = "happy";
string str2 = "ahppy";
if (isPermutation(&str1, &str2))
{
cout<<"yes"<<endl;
} else cout<<"no"<<endl;
}
Comments
Post a Comment