xxxxxxxxxx
public static List<string> SubString(String str)
{
var list = new List<string>();
for (int i = 0; i < str.Length; i++)
{
for (int j = 1; j <= str.Length - i; j++)
{
var temp = str.Substring(i, j);
if(temp==ReverseString(temp))
{
list.Add(temp);
}
}
}
return list;
}
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
xxxxxxxxxx
<script>
// A O(n^2) time and O(1) space program to
// find the longest palindromic substring
// easy to understand as compared to previous version.
// A utility function to print
// a substring str[low..high]
// This function prints the
// longest palindrome substring (LPS)
// of str[]. It also returns the
// length of the longest palindrome
function longestPalSubstr(str)
{
let n = str.length; // calculating size of string
if (n < 2)
return n; // if string is empty then size will be 0.
// if n==1 then, answer will be 1(single
// character will always palindrome)
let maxLength = 1,start=0;
let low, high;
for (let i = 0; i < n; i++) {
low = i - 1;
high = i + 1;
while ( high < n && str[high] == str[i]) //increment 'high'
high++;
while ( low >= 0 && str[low] == str[i]) // decrement 'low'
low--;
while (low >= 0 && high < n && str[low] == str[high]){
low--;
high++;
}
let length = high - low - 1;
if (maxLength < length) {
maxLength = length;
start=low+1;
}
}
document.write("Longest palindrome substring is: ");
document.write(str.substring(start,maxLength+start));
return maxLength;
}
// Driver program to test above functions
let str = "forgeeksskeegfor";
document.write("</br>","Length is: " + longestPalSubstr(str),"</br>");
// This is code is contributed by shinjanpatra
</script>
xxxxxxxxxx
public int longestPalindromeSubseq(String s) {
String s2=reverse(s);
int n=s.length();
int m=s2.length();
int dp[][]=new int[n+1][m+1];
for(int i=0;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
if(i==0 || j==0)
{
dp[i][j]=0;
}
else
{
if(s.charAt(i-1)==s2.charAt(j-1))
dp[i][j]=1+dp[i-1][j-1];
else
{
dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
}
}
}
}
return dp[n][m];
}
}
xxxxxxxxxx
public String longest_palindrome(String s) {
// if length of string is less than 1 return empty string
if (s == null || s.length() < 1) return "";
int start = 0, end = 0;
for (int i = 0; i < s.length(); i++) {
int len1 = expand_around_center(s, i, i);
int len2 = expand_around_center(s, i, i + 1);
int len = Math.max(len1, len2);
if (len > end - start) {
start = i - (len - 1) / 2;
end = i + len / 2;
}
}
// return longest palindromic substring
return s.substring(start, end + 1);
}
private int expand_around_center(String s, int left, int right) {
int L = left, R = right;
while (L >= 0 && R < s.length() && s.charAt(L) == s.charAt(R)) {
L--;
R++;
}
return R - L - 1;
}
public static void main(){
String word = "findnitianhere";
System.out.println(longest_palindrome(word));
}
xxxxxxxxxx
/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = function(s) {
};
xxxxxxxxxx
class Solution {
/**
* @param String $s
* @return String
*/
function longestPalindrome($s) {
}
}