Write a program in java to print total palindrome strings presented in given string.
Input : abcacbbbca
Output : [bb, acbbbca, bbb, cac, cbbbc, bcacb]
import java.util.*;
class Palindrome3
{
public static void main(String[] args)
{
String s="abcacbbbca";
String s2="";
HashSet<String> hs=new HashSet<>();
for(int i=0;i<s.length();i++)
{
for(int j=i+2;j<=s.length();j++)
{
s2=s.substring(i,j);
StringBuffer sb=new StringBuffer(s2).reverse();
if(s2.equals(sb.toString()))
hs.add(s2);
}
}
System.out.print("Total Palindromes = "+hs);
}
}
Write a program in java to print total palindrome strings presented in given string. [All Java Interview Programs] |
0 Comment to "Write a program in java to print total palindrome strings presented in given string. Input : abcacbbbca Output : [bb, acbbbca, bbb, cac, cbbbc, bcacb]"
Post a Comment