0

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
keremispirli
  • 3,283
  • 2
  • 19
  • 26
Nani
  • 9
  • 4
  • 1
    You need an outer loop iterating over different start positions and lengths. In the simplest approach, that's O(N^2), but makes things simple. – PMF Dec 19 '22 at 12:26
  • It can be of help: https://stackoverflow.com/questions/2677000/how-to-find-the-longest-palindrome-in-a-given-string – Majid Shahabfar Dec 19 '22 at 12:54

3 Answers3

1

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
Wexos
  • 71
  • 4
0

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
Vinay Soni
  • 88
  • 1
  • 6
0
import java.util.ArrayList;
import java.util.List;

public class PalindromeSubStrings {
    private static boolean isPalindrome(String s){
        if(s.length()<2)
            return false;
        return s.equals(new StringBuilder(s).reverse().toString());
    }
    
    public static void main(String[] args) {
        String s1 = "ab12321bakjjkh3432";
        List<String> palindromeList = new ArrayList<>();

        for (int i=0; i<=s1.length(); i++){
            for(int j=i+1; j<=s1.length(); j++){
                if(isPalindrome(s1.substring(i,j))){
                    palindromeList.add(s1.substring(i,j));
                }
            }
        }

        palindromeList.forEach(
                s -> {
                    System.out.println("Palindrome strings are:: " + s + ", ");
                }
        );
    }
}
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Mar 09 '23 at 01:05