Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions 1-Array-And-String/CheckPermutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,14 @@ bool checkPermutation( string string1, string string2 ) {
if( string1.length() != string2.length() ) {
return false;
}
int charMap[128] = {0};
for( int i=0; i < string1.length(); i++ ) {
charMap[ string1[ i ] ]++;
int xorValue=0;
for(char ch : string1){
xorValue^=ch;
}

for( int i=0; i < string2.length(); i++ ) {
if( charMap[ string2[ i ] ] == 0 ) {
return false;
}
charMap[ string2[ i ] ]--;
for(char ch : string2){
xorValue^=ch;
}
return true;
return xorValue==0;
}

bool checkPermutationUsingSorting( string input1, string input2 ) {
Expand Down
11 changes: 5 additions & 6 deletions 1-Array-And-String/IsUnique.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ bool isDupUsingHashTable( string input ) {
return true;
}

bool map[ 128 ] = { false };
for( int i=0; i < input.length(); i++ ) {
if( map[ input[ i ] ] == true )
return true;
else
map[ input[ i ] ]= true;
for(int i=0;i<input.length();i++){
for(int j=i+1;j<input.length();j++){
if(input[i]==input[j])
return true;
}
}
return false;
}
Expand Down
36 changes: 13 additions & 23 deletions 1-Array-And-String/PalindromePermutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,23 @@
using namespace std;

bool canFormPalindrome( string input ) {
int charFreqCount[ 26 ] = {0};
int charFreqCount[26] = {0};

int charCount = 0;
for( int i = 0; i < input.length(); i++ ) {
if( input[ i ] == ' ' ) {
continue;
for (char c : input) {
if (isspace(c)) continue;
if (isalpha(c)) {
charFreqCount[tolower(c) - 'a']++;
}
}
if( 'a' <= input[ i ] && input[ i ] <= 'z' ) {
charFreqCount[ input[ i ] - 'a' ]++;
charCount++;
continue;

int oddCount = 0;
for (int i = 0; i < 26; i++) {
if (charFreqCount[i] % 2 != 0) {
oddCount++;
}
}
if( 'A' <= input[ i ] && input[ i ] <= 'Z' ) {
charFreqCount[ input[ i ] - 'A' ]++;
charCount++;
}
}

int oddCount = 0;
for( int i=0; i < 26; i++ ) {
if( charFreqCount[ i ] % 2 != 0 )
oddCount++;
}

if( oddCount == 0 && charCount % 2 == 0 ) {
// when char count is even but oddCount is zero.
return true;
return oddCount <= 1;
}

if( charCount % 2 != 0 && oddCount == 1 ) {
Expand Down