Skip to content

Commit 6007708

Browse files
checkpermutation in o(1)
1 parent 102cdbc commit 6007708

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

1-Array-And-String/CheckPermutation.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,14 @@ bool checkPermutation( string string1, string string2 ) {
1818
if( string1.length() != string2.length() ) {
1919
return false;
2020
}
21-
int charMap[128] = {0};
22-
for( int i=0; i < string1.length(); i++ ) {
23-
charMap[ string1[ i ] ]++;
21+
int xorValue=0;
22+
for(char ch : string1){
23+
xorValue^=ch;
2424
}
25-
26-
for( int i=0; i < string2.length(); i++ ) {
27-
if( charMap[ string2[ i ] ] == 0 ) {
28-
return false;
29-
}
30-
charMap[ string2[ i ] ]--;
25+
for(char ch : string2){
26+
xorValue^=ch;
3127
}
32-
return true;
28+
return xorValue==0;
3329
}
3430

3531
bool checkPermutationUsingSorting( string input1, string input2 ) {

0 commit comments

Comments
 (0)