I'm asked to check whether a given string can be a palindrome after rearranging and then return true if it can be a palindrome or false if it cannot be a palindrome.
I'm getting a runtime error: Segmentation fault while running tests.
Here's my code:
bool palindromeRearranging(char * inputString) {
int index;
int count, count1 = 0;
for(int i = 0, b = strlen(inputString); i < b -1 ; i++)
{ count = 0;
if(inputString[i] == '*')
{
continue;
}else
{
for(int j = i+1; j < b ;j++)
{
if(inputString[i] == inputString[j] )
{
inputString[j] = '*';
count++;
index = i;
}
}
inputString[index] = '*';
}
if(count %2 == 0 && count != 0)
{
count1++;
}
}
for(int i = 0, b = strlen(inputString); i < b; i++)
{
if(inputString[i] != '*')
{
count1++;
}
}
if(count1 > 1)
{
return false;
}else
{
return true;
}
}
Here inputString
is the string given. I tried to replace the two characters with *
if they are both same. And then counting the no. of single characters.
5 out of 10 test cases have passed.
For example test cases like "abbcabb"
, "aabb"
, "zyyzzzzz"
have passed.
But I'm getting runtime error: Segmentation fault for test cases like "abcad"
, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbccccaaaaaaaaaaaaa"
. "abca"
, "abdhuierf"
.
I hope you could help me with this one.
Btw I'm solving this on codesignal.com, under the arcade section/intro. Q.18 palindromeRearranging.