Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

palindromic substrings

	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);
    }
Comment

Longest Palindromic Substring

public class Solution {
    public string LongestPalindrome(string s) {
        
    }
}
Comment

longest palindromic substring

<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>
Comment

longest palindromic subsequence

 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];
    }
}
Comment

longest palindromic substring

/*
	This implementation demonstrates how to 
	find the longest palindrome substring 
	in a given bigger string. 

	
	Let n be the length of the bigger string.

	Time complexity: O(n^2) 
	Space complexity: O(1)
*/
public class LongestPalindromeSubstring {

	public static void main(String[] args) {
		String s = "babad";
		System.out.println(longestPalindrome(s)); // bab
	}

	private static String longestPalindrome(String s) {
		if (s.length() == 1) {
			return s;
		}

		int maxLength = Integer.MIN_VALUE;
		String longestPalindrome = "";
		int n = s.length();

		for (int index = 0; index < n; index++) {
			for (int x = 0; index - x >= 0 && index + x < n; x++) {
				if (s.charAt(index - x) == s.charAt(index + x)) {
					int len = 2 * x + 1;
					if (len > maxLength) {
						maxLength = len;
						longestPalindrome = s.substring(index - x, index + x + 1);
					}
				} else {
					break;
				}
			}
		}

		for (int index = 0; index < n - 1; index++) {
			for (int x = 1; index - x + 1 >= 0 && index + x < n; x++) {
				if (s.charAt(index - x + 1) != s.charAt(index + x)) {
					break;
				} else {
					int len = 2 * x;
					if (len > maxLength) {
						maxLength = len;
						longestPalindrome = s.substring(index - x + 1, index + x + 1);
					}
				}
			}
		}

		return longestPalindrome;
	}
}
Comment

Longest Palindromic Substring

class Solution {
public:
    string longestPalindrome(string s) {
        
    }
};
Comment

Longest Palindromic Substring

class Solution {
    public String longestPalindrome(String s) {
        
    }
}
Comment

Longest Palindromic Substring



char * longestPalindrome(char * s){

}
Comment

Longest Palindromic Substring

/**
 * @param {string} s
 * @return {string}
 */
var longestPalindrome = function(s) {
    
};
Comment

Longest Palindromic Substring

# @param {String} s
# @return {String}
def longest_palindrome(s)
    
end
Comment

Longest Palindromic Substring

class Solution {

    /**
     * @param String $s
     * @return String
     */
    function longestPalindrome($s) {
        
    }
}
Comment

Longest Palindromic Substring

function longestPalindrome(s: string): string {

};
Comment

Longest Palindromic Substring

class Solution {
    func longestPalindrome(_ s: String) -> String {
        
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: loop in c# 
Csharp :: triangle 
Csharp :: if else c# 
Csharp :: c# double 
Csharp :: raycasting unity 
Csharp :: combined 2 arrays 
Csharp :: c# webclient vs httpclient 
Csharp :: how to use var in c# 
Csharp :: f# get last element of list 
Csharp :: Entity framwork update parent entity added new sub entity 
Csharp :: void on TriggerCollisionEnter2D 
Csharp :: if exercises c# 
Csharp :: Delayed respawn timer 
Csharp :: c# predicate 
Csharp :: C# print all properties of an object including children objects 
Csharp :: find the values of dictionaries in C sharp 
Csharp :: Handling Collisions unity 
Csharp :: c# accept any enum 
Csharp :: save and query mongodb collection as dynamic ExpandoObject 
Csharp :: tune off exit button wpf 
Csharp :: [Package Manager Window] Error while fetching labels: User is not logged in or user status invalid. UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&) 
Csharp :: Razor break/continue in loop 
Csharp :: firefoxoptions setpreference to trust certificates 
Csharp :: how to show enum name list from input in swagger c# 
Csharp :: tostring vb.net format decimal value with comma 
Csharp :: imageLocation in C# 
Csharp :: switch rows and columns in a datagridview 
Csharp :: backcolor app winform C3 
Csharp :: unity how to set framrate C# 
Csharp :: world space constant size 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =