Home > Back-end >  How to get all palindrome substrings from the given string "ab12321bakjjkh3432"?
How to get all palindrome substrings from the given string "ab12321bakjjkh3432"?

Time:12-20

I tried with this code, but it only displays whether a given string is palindrome or not. I want to extract and display all the possible palindrome substrings in the given string.

public static boolean istPalindrom(char[] word){
    int i1 = 0;
    int i2 = word.length - 1;
    while (i2 > i1) {
        if (word[i1] != word[i2]) {
            return false;
        }
          i1;
        --i2;
    }
    return true;
}

Expected output:

  1. 232
  2. 12321
  3. b12321b
  4. ab12321ba
  5. 343
  6. kjjk

CodePudding user response:

Here is an example:

using System;
        
public class Program
{
    private static bool istPalindrom(string word){
        int i1 = 0;
        int i2 = word.Length - 1;
        while (i2 > i1) {
            if (word[i1] != word[i2]) {
                return false;
            }
              i1;
            --i2;
        }
        return true;
    }
    
    private static void FindPalindromes(string s)
    {
        // Assume a palindrome string is at least 2 characters
        const int MinLength = 2;
        
        if (s.Length <= MinLength)
        {
            return;
        }
        
        // Test all substrings by removing i first characters
        for (int i = 0; i < s.Length - MinLength; i  )
        {
            string sub = s.Substring(i);
            
            if (istPalindrom(sub))
            {
                Console.WriteLine($"Found palindrome: {sub}");  
            }
        }
        
        // Test all strings by the last character
        FindPalindromes(s.Substring(0, s.Length - 1));
    }
    
    public static void Main()
    {
        FindPalindromes("ab12321bakjjkh3432");
    }
}

which uses your istPalindrom method to check (but with string instead of char[]). This tests all possible substrings of the given string. There are probably more efficient ways to do this, taking advantage of a palindrome of length n must contain a palindrom of length n - 2, meaning one could find all palindromes of length 3 and 4, and then try expanding those strings.

Output:

Found palindrome: 343
Found palindrome: kjjk
Found palindrome: ab12321ba
Found palindrome: b12321b
Found palindrome: 12321
Found palindrome: 232

CodePudding user response:

Palindrome initial string

public static string PalindromeString { get; set; } = "ab12321bakjjkh3432";

Actual logic to print palindrome string from given string

 for (float index = 0; index < PalindromeString.Length; index  = (float).5)
        {
            // set nearest element radius
            // on both left and right side
            float palindromeNearestElementRadius = index - (int)index;

            // if there is need to compare indexes and if it has desired elements
            // and both sides of value matches for Example (212) etc.....
            while ((index   palindromeNearestElementRadius) < PalindromeString.Length
                && (index - palindromeNearestElementRadius) >= 0
                && PalindromeString[(int)(index - palindromeNearestElementRadius)]
                        == PalindromeString[(int)(index   palindromeNearestElementRadius)])
            {
                var element = PalindromeString.Substring((int)(index - palindromeNearestElementRadius),
                            (int)(index   palindromeNearestElementRadius   1) -
                            (int)(index - palindromeNearestElementRadius));

                 if (element.Length != 1)
                {
                    Console.WriteLine(element);
                }

                // increasing the element radius by 1
                // to point towards the
                // next elements for both sides to move forward our comparision
                //
                palindromeNearestElementRadius  ;
            }

Output

232
12321
b12321b
ab12321ba
jj
kjjk
343
  • Related