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 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 :: enum in combobox wpf 
Csharp :: c# list contains null 
Csharp :: How to get selected item from Dropdown in GridView 
Csharp :: Popup open close wpf 
Csharp :: unity script template folder 
Csharp :: unity3d gameobject follow path 
Csharp :: run in wpf 
Csharp :: oncollisionenter2d 
Csharp :: trygetvalue c# 
Csharp :: summernote dropdown plugin 
Csharp :: if or statement c# 
Csharp :: c# mysql select into datatable 
Csharp :: c# datetime 
Csharp :: C# Convert xml to datatable 
Csharp :: c# switch example 
Csharp :: get xml from url 
Csharp :: linq syntax 
Csharp :: c# copy an object 
Csharp :: register all services microsoft .net core dependency injection container 
Csharp :: Create a list of 3 Orders c# 
Csharp :: C# ToCsv Extension Method 
Csharp :: how to make a chunk loader in c# 
Csharp :: lock a cache in asp.net 
Csharp :: erewt 
Csharp :: how to change argument of function in f# 
Csharp :: Get single listView SelectedItem 
Csharp :: how to system func bool unity 
Csharp :: add RowDefinition from cs xamarin 
Csharp :: whining 
Csharp :: convert enum to keyvalue 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =