Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 219469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:50:52+00:00 2026-05-11T18:50:52+00:00

Given a string, figure out how many characters minimum are needed to make the

  • 0

Given a string, figure out how many characters minimum are needed to make the word a palindrome. Examples:

ABBA : 0 (already a palindrome)
ABB: 1
FAE: 2
FOO: 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:50:52+00:00Added an answer on May 11, 2026 at 6:50 pm

    Algorithms only, since this is probably homework [Apologies to Raymond, it’s an interview question rather than homework, as his edits/comments make clear. However, the algorithms and added pseudo-code are still valid for that purpose, and I’ve added some C code at the end].

    You need to find the longest palindrome at the end of the string. An algorithm to see if a string is a palindrome can be created by simply running one pointer from the start of the string and one from the end, checking that the characters they refer to are identical, until they meet in the middle. Something like:

    function isPalindrome(s):
        i1 = 0
        i2 = s.length() - 1
        while i2 > i1:
            if s.char_at(i1) not equal to s.char_at(i2):
                return false
            increment i1
            decrement i2
        return true
    

    Try that with the full string. If that doesn’t work, save the first character on a stack then see if the remaining characters form a palindrome. If that doesn’t work, save the second character as well and check again from the third character onwards.

    Eventually you’ll end up with a series of saved characters and the remaining string which is a palindrome.

    Best case is if the original string was a palindrome in which case the stack will be empty. Worst case is one character left (a one-character string is automatically a palindrome) and all the others on the stack.

    The number of characters you need to add to the end of the original string is the number of characters on the stack.

    To actually make the palindrome, pop the characters off the stack one-by-one and put them at the start and the end of the palindromic string.

    Examples:

    String      Palindrome  Stack  Notes
    ------      ----------  -----  -----
    ABBA            Y       -      no characters needed.
    
    String      Palindrome  Stack  Notes
    ------      ----------  -----  -----
    ABB             N       -
    BB              Y       A      one character needed.
    ABBA            Y       -      start popping, finished.
    
    String      Palindrome  Stack  Notes
    ------      ----------  -----  -----
    FAE             N       -
    AE              N       F
    E               Y       AF     two characters needed.
    AEA             Y       F      start popping.
    FAEAF           Y       -      finished.
    
    String      Palindrome  Stack  Notes
    ------      ----------  -----  -----
    FOO             N       -
    OO              Y       F      one character needed.
    FOOF            Y       -      start popping, finished.
    
    String      Palindrome  Stack  Notes
    ------      ----------  -----  -----
    HAVANNA         N       -
    AVANNA          N       H
    VANNA           N       AH
    ANNA            Y       VAH    three characters needed.
    VANNAV          Y       AH     start popping.
    AVANNAVA        Y       H
    HAVANNAVAH      Y       -      finished.
    

     

    String          Palindrome   Stack      Notes
    ------          ----------   --------   -----
    deoxyribo           N        -
    eoxyribo            N        d
    oxyribo             N        ed
    :                   :        :
    bo                  N        iryxoed
    o                   Y        biryxoed   eight chars needed.
    bob                 Y        iryxoed    start popping.
    ibobi               Y        ryxoed
    :                   :        :
    oxyribobiryxo       Y        ed
    eoxyribobiryxoe     Y        d
    deoxyribobiryxoed   Y        -          finished.
    

    Converting this method to “code”:

    function evalString(s):
        stack = ""
        while not isPalindrome(s):
            stack = s.char_at(0) + stack
            s = s.substring(1)
        print "Need " + s.length() + " character(s) to make palindrome."
        while stack not equal to "":
            s = stack.char_at(0) + s + stack.char_at(0)
            stack = stack.substring(1)
        print "Palindrome is " + s + "."
    

    For those less interested in pseudo-code, here’s a test program in C which does the trick.

    #include <stdio.h>
    #include <string.h>
    
    static char *chkMem (char *chkStr) {
        if (chkStr == NULL) {
            fprintf (stderr, "Out of memory.\n");
            exit (1);
        }
        return chkStr;
    }
    
    static char *makeStr (char *oldStr) {
        char *newStr = chkMem (malloc (strlen (oldStr) + 1));
        return strcpy (newStr, oldStr);
    }
    
    static char *stripFirst (char *oldStr) {
        char *newStr = chkMem (malloc (strlen (oldStr)));
        strcpy (newStr, &(oldStr[1]));
        free (oldStr);
        return newStr;
    }
    
    static char *addFront (char *oldStr, char addChr) {
        char *newStr = chkMem (malloc (strlen (oldStr) + 2));
        sprintf (newStr, "%c%s", addChr, oldStr);
        free (oldStr);
        return newStr;
    }
    

     

    static char *addBoth (char *oldStr, char addChr) {
        char *newStr = chkMem (malloc (strlen (oldStr) + 3));
        sprintf (newStr, "%c%s%c", addChr, oldStr, addChr);
        free (oldStr);
        return newStr;
    }
    
    static int isPalindrome (char *chkStr) {
        int i1 = 0;
        int i2 = strlen (chkStr) - 1;
        while (i2 > i1)
            if (chkStr[i1++] != chkStr[i2--])
                return 0;
        return 1;
    }
    

     

    static void evalString (char *chkStr) {
        char * stack = makeStr ("");
        char * word = makeStr (chkStr);
    
        while (!isPalindrome (word)) {
            printf ("%s: no, ", word);
            stack = addFront (stack, *word);
            word = stripFirst (word);
            printf ("stack <- %s, word <- %s\n", stack, word);
        }
        printf ("%s: yes, need %d character(s)\n", word, strlen (stack));
    
        printf ("----------------------------------------\n");
        printf ("Adjusting to make palindrome:\n");
        while (strlen (stack) > 0) {
            printf ("   %s, stack <- %s\n", word, stack);
        word = addBoth (word, *stack);
        stack = stripFirst (stack);
        }
        printf ("   %s\n", word);
        printf ("========================================\n");
    
        free (word);
        free (stack);
    }
    
    int main (int argc, char *argv[]) {
        int i;
        for (i = 1; i < argc; i++) evalString (argv[i]);
        return 0;
    }
    

    Running this with:

    mkpalin abb abba fae foo deoxyribo
    

    gives the output:

    abb: no, stack <- a, word <- bb
    bb: yes, need 1 character(s)
    ----------------------------------------
    Adjusting to make palindrome:
       bb, stack <- a
       abba
    ========================================
    

     

    abba: yes, need 0 character(s)
    ----------------------------------------
    Adjusting to make palindrome:
       abba
    ========================================
    

     

    fae: no, stack <- f, word <- ae
    ae: no, stack <- af, word <- e
    e: yes, need 2 character(s)
    ----------------------------------------
    Adjusting to make palindrome:
       e, stack <- af
       aea, stack <- f
       faeaf
    ========================================
    

     

    foo: no, stack <- f, word <- oo
    oo: yes, need 1 character(s)
    ----------------------------------------
    Adjusting to make palindrome:
       oo, stack <- f
       foof
    ========================================
    

     

    deoxyribo: no, stack <- d, word <- eoxyribo
    eoxyribo: no, stack <- ed, word <- oxyribo
    oxyribo: no, stack <- oed, word <- xyribo
    xyribo: no, stack <- xoed, word <- yribo
    yribo: no, stack <- yxoed, word <- ribo
    ribo: no, stack <- ryxoed, word <- ibo
    ibo: no, stack <- iryxoed, word <- bo
    bo: no, stack <- biryxoed, word <- o
    o: yes, need 8 character(s)
    ----------------------------------------
    Adjusting to make palindrome:
       o, stack <- biryxoed
       bob, stack <- iryxoed
       ibobi, stack <- ryxoed
       ribobir, stack <- yxoed
       yribobiry, stack <- xoed
       xyribobiryx, stack <- oed
       oxyribobiryxo, stack <- ed
       eoxyribobiryxoe, stack <- d
       deoxyribobiryxoed
    ========================================
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Categories extend the original class, but they don't subclass it,… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer Haven't tested this, but it's something like: RewriteRule \.php$ -… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer "0:0:0:0:0:0:0:1" is the IPv6 loopback address as defined in RFC… May 12, 2026 at 12:54 pm

Related Questions

I've read a few questions on SO (such as this one ) in regards
I'm playing around with MySQLi at the moment, trying to figure out how it
I have a simple application using netbeans for developing and maven for building et
I have recently started using Google Webmaster Tools . I was quite surprised to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.