# Given a string of lowercase ASCII characters, find all distinct continuous palindromic sub-strings of it.

• void palindromeSubStrs(string s)
{
map<string, int> m;
int n = s.size();

``````// table for storing results (2 rows for odd-
// and even-length palindromes
int R[2][n+1];

// Find all sub-string palindromes from the given input
// string insert 'guards' to iterate easily over s
s = "@" + s + "#";

for (int j = 0; j <= 1; j++)
{
int rp = 0;   // length of 'palindrome radius'
R[j][0] = 0;

int i = 1;
while (i <= n)
{
//  Attempt to expand palindrome centered at i
while (s[i - rp - 1] == s[i + j + rp])
rp++;  // Incrementing the length of palindromic
// radius as and when we find vaid palindrome

// Assigning the found palindromic length to odd/even
// length array
R[j][i] = rp;
int k = 1;
while ((R[j][i - k] != rp - k) && (k < rp))
{
R[j][i + k] = min(R[j][i - k],rp - k);
k++;
}
rp = max(rp - k,0);
i += k;
}
}

// remove 'guards'
s = s.substr(1, n);

// Put all obtained palindromes in a hash map to
// find only distinct palindromess
m[string(1, s[0])]=1;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= 1; j++)
for (int rp = R[j][i]; rp > 0; rp--)
m[s.substr(i - rp - 1, 2 * rp + j)]=1;
m[string(1, s[i])]=1;
}

//printing all distinct palindromes from hash map
``````

cout << "Below are " << m.size()-1
<< " palindrome sub-strings";
map<string, int>::iterator ii;
for (ii = m.begin(); ii!=m.end(); ++ii)
cout << (*ii).first << endl;
}

• For better view,put all the code in same place.

``````void palindromeSubStrs(string s) {

map<string, int> m; int n = s.size();
// table for storing results (2 rows for odd-
// and even-length palindromes
int R[2][n+1];

// Find all sub-string palindromes from the given input
// string insert 'guards' to iterate easily over s
s = "@" + s + "#";

for (int j = 0; j <= 1; j++)
{
int rp = 0;   // length of 'palindrome radius'
R[j][0] = 0;

int i = 1;
while (i <= n)
{
//  Attempt to expand palindrome centered at i
while (s[i - rp - 1] == s[i + j + rp])
rp++;  // Incrementing the length of palindromic
// radius as and when we find vaid palindrome

// Assigning the found palindromic length to odd/even
// length array
R[j][i] = rp;
int k = 1;
while ((R[j][i - k] != rp - k) && (k < rp))
{
R[j][i + k] = min(R[j][i - k],rp - k);
k++;
}
rp = max(rp - k,0);
i += k;
}
}

// remove 'guards'
s = s.substr(1, n);

// Put all obtained palindromes in a hash map to
// find only distinct palindromess
m[string(1, s[0])]=1;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= 1; j++)
for (int rp = R[j][i]; rp > 0; rp--)
m[s.substr(i - rp - 1, 2 * rp + j)]=1;
m[string(1, s[i])]=1;
}

//printing all distinct palindromes from hash map
cout << "Below are " << m.size()-1 << " palindrome sub-strings"; map<string, int>::iterator ii; for (ii = m.begin(); ii!=m.end(); ++ii) cout << (*ii).first << endl;
}``````

• My C# solution

``````class QContinuousPalindrome
{
public static IList<string> GetDistinctPalindromics(string str)
{
Dictionary<string, int> palindromicCounter = new Dictionary<string, int>();

int rp = 0;

for (int i = 0; i < str.Length; i++)
{
rp = 0;
while(i-rp >=0 && i+rp < str.Length)
{
if (IsPalindromic(str, i - rp, i + rp))
AddIntoDictionary(str.Substring(i - rp, 2 * rp + 1), palindromicCounter);
if (IsPalindromic(str, i - rp, i + rp + 1))
AddIntoDictionary(str.Substring(i - rp, 2 * rp + 2), palindromicCounter);
rp++;
}
}

return palindromicCounter.Keys.ToList();
}

private static void AddIntoDictionary(string str, Dictionary<string,int> dict)
{
if (dict.ContainsKey(str))
dict[str]++;
else
}
private static bool IsPalindromic(string str, int start, int end)
{
if (start < 0 || end >= str.Length)
return false;

while (start <= end)
{
if (str[start] != str[end])
return false;
else
{
start++;
end--;
continue;
}
}
return true;
}
}
``````

• I dont know why people have used three for loops and what not if you can actually do it in two loops. Please correct me or advice me if you find mistakes in the code below.This is my first answer to leetcode.

`````` *
*/
package Solutions;

import java.util.ArrayList;

/**
* @author ****
*
*/
public class ReversePalindSubStrings {

/**
* @param args
*/
public static void main(String[] args)
{
String s="sabollolpiplastcruelpopblacksheeparoundioihingerigoroussas";
int startindex=0;
int endIndex=2;
StringBuilder temp=new StringBuilder();
StringBuilder revtemp=new StringBuilder();
ArrayList<String> palindSubString=new ArrayList<String>();
for(int i=0;i<s.length();i++)
{
for(int j=i;j<s.length()-1;j++)
{
String original=temp.append(s.substring(startindex+i, endIndex+j)).toString();
if(compare(original,temp.reverse()))
{
}
temp.setLength(0);
}
temp.setLength(0);
}
for(String str:palindSubString)
{
System.out.println(str);
}
}
public static boolean compare(String str1,StringBuilder str2)
{
for(int i=0;i<str1.length();i++)
{
if(!(str1.charAt(i)==str2.charAt(i)))
{
return false;
}
}
return true;
}
``````

}

• Code with just 2 for loops.

``````import java.util.Stack;
public class PalSubString {
public static boolean compare(String str1,StringBuilder str2)
{
for(int i=0;i<str1.length();i++)
{
if(!(str1.charAt(i)==str2.charAt(i)))
{
return false;
}
}
return true;
}
public static void main(String[] args) {
String s= "eerfetasdfghjkllkjhgfdsaytr";
String s1 = new String();
Stack<String> stack = new Stack<String>();
for(int i=0;i<s.length();i++)
{
for(int j=i+1;j<s.length();j++)
{

s1 = "";
s1 = (s.substring(i, j));
StringBuilder original =  new StringBuilder(s1);
if(compare(s1, original.reverse()))
{
if(stack.isEmpty())
{
stack.push(original.toString());
}
else{
if(stack.peek().length() == original.toString().length())
{
}
else if(stack.peek().length() < original.toString().length())
{
stack.clear();
}
else
{
//Do Nothing
}
}
}
}
}
System.out.println(stack.pop().toString());
}
}

``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.