2

I was looking for method to find efficient way to check if a string is rotated palindrome or not and apart from O(n^2) solution I found SO post which talks of O(n) solution which seems quite complex. I have written below code and I think it achieves the solution in O(n) as well in a much simpler hash table format.Can someone have a look at it and see if I am missing something here or my solution is correct ?

/*Code doesnt cover scenarios where character is caps and space*/

int main(void)
{
    char str[] = "123217898";
    char *pstr = str;
    int length = strlen(str);
    int notdouble = 0;


    int arr[256] = { 0 };

    for (int i = 0; i < length; i++)
    {
        arr[*pstr]++;
        pstr++;
    }

    pstr = str;

    for (int i = 0; i < length; i++)
    {
        /*check if its divisible by 2 and greater than 2*/
        if (arr[*pstr] % 2 != 0 && arr[*pstr] > 2)
        {
            notdouble = 5;   // just assign random value greater than 1
        }

        if (arr[*pstr] == 1)
        {
            notdouble++;
        }
        pstr++;
    }

    if (notdouble > 1)
    {
        printf("string is not palindrome \n");
    }
    else
    {
        printf("string is palindrome \n");
    }

    return 0;
}
Community
  • 1
  • 1
oneday
  • 629
  • 1
  • 9
  • 32
  • 5
    I'm voting to close this question as off-topic because it's about code-review (try http://codereview.stackexchange.com). – Oliver Charlesworth Jan 10 '16 at 23:15
  • 1
    Previously asked [question](http://stackoverflow.com/questions/12286091/efficient-way-to-verify-if-a-string-is-a-rotated-palindrome) – Weather Vane Jan 10 '16 at 23:16
  • @WeatherVane - I myself have provided that link - question talks about alternative solution which is much simpler. I am looking for answer to check if there is some hole in my logic because no one has suggested it – oneday Jan 10 '16 at 23:18
  • Oh - sorry. It was the top entry when I googled "rotated palindrome" – Weather Vane Jan 10 '16 at 23:19
  • @OliverCharlesworth - More than code review I am looking for validation of part of logic which provides much simpler alternative than the complex method associated. If people dont agree here I dont mind posting it on code review .. – oneday Jan 10 '16 at 23:19
  • This may be a good question for [codereview.se], so long as: **(A)** _the code works_, **and (B)** _it's not hypothetical or incomplete in any way_. Please read the [on-topic guide](http://codereview.stackexchange.com/help/on-topic) before posting, if you choose to go to [Code Review](http://codereview.stackexchange.com/questions/ask). If you have any questions or concerns, join us at our [CR Help Desk](http://chat.stackexchange.com/rooms/34045). – Quill Jan 10 '16 at 23:23
  • @Quill - Sure I will post there then ..thanks !! – oneday Jan 10 '16 at 23:25
  • Your code doesn't seem to solve the problem you are describing, as it just tests whether characters are repeated or not... It shouldn't be hard to provide a string which repeats characters, but are neither a palindrome nor a rotated palindrome. I.e. '123321', rotated: '211233', not legal: '221133'. Not tested, but the last should falsely be reported. – holroy Jan 10 '16 at 23:26
  • @holroy - yes thats the logic I am pursuing that as long as all characters are multiple of two with max 1 character not being multiple of 2 its a palindrome. Curious why would string '221133' not be rotated palindrome ? – oneday Jan 10 '16 at 23:27
  • There's a reasonably simple expected linear-time solution that uses Rabin fingerprints, but this is not that. – David Eisenstat Jan 10 '16 at 23:31
  • @oneday, How would you rotate it to become a palindrome? 322113? 332211?, 133221? 113322? 211332? Niether of these are palindromic in the normal sense of palindromes – holroy Jan 10 '16 at 23:32
  • @oneday: Recall that, when not rotated, a palindrome is a string that reads the same from either end. You can't rotate '221133' to form a palindrome. – 500 - Internal Server Error Jan 10 '16 at 23:32
  • @holroy - I see ur point - what I did was tested with bunch of palindrome words from google and some random words and it turned out true - but I was skeptical of solution myself :) so the post. thanks for the answer it does show my solution is wrong – oneday Jan 10 '16 at 23:36
  • @500-InternalServerError - got the point - solves my confusion. – oneday Jan 10 '16 at 23:37

1 Answers1

3

Your code does not test for a rotated palindrome, it merely checks if there is a even amount of repeated characters. That is not conformant with being a palindrome, nor a rotated palindrome.

In addition, a palindrome or rotated palindrome can contain one character with an odd count.

In other words the following two examples are wrongly classified:

  • "221133" – This is not a rotated palindrome, even though there is even number of repeated characters.
  • "114321234" - This a rotated palindrome, but the odd count of 1's is three, not one...
holroy
  • 3,047
  • 25
  • 41
  • thanks for the pointer. it seems with rotation I assumed all permutation will occur once and thats where the algorithm fails. and yes as u pointed out there can be instances where one character count can be 3 as well ( which I think I can hack into code) but that wont solve problem as my code assumes all permutation. thanks once again !!! – oneday Jan 11 '16 at 03:20
  • The code checks for odd concurrence counts, allowing, if anything, exactly _one_ character occurring once: _permuted_ palindrome (with not all palindrome anagrams accepted). – greybeard Jan 13 '16 at 10:13
  • @greybeard yes, but when allowing multiple occurrences of characters (and rotation) the total may be an odd number of characters – holroy Jan 13 '16 at 11:45
  • @greybeard the following is a palindrome: 555123454321555, where the number of 5's is odd and not one. – holroy Jan 13 '16 at 11:49
  • @greybeard that is rotated. Move the first character to the end, and you'll see the palindrome – holroy Jan 13 '16 at 11:53