Longest Palindromic Substring

package com.thealgorithms.strings;

// Longest Palindromic Substring
import java.util.Scanner;

class LongestPalindromicSubstring {

    public static void main(String[] args) {
        Solution s = new Solution();
        String str = "";
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the string: ");
        str = sc.nextLine();
        System.out.println("Longest substring is : " + s.longestPalindrome(str));
    }
}

class Solution {

    public String longestPalindrome(String s) {
        if (s == null || s.length() == 0) {
            return "";
        }
        int n = s.length();
        String maxStr = "";
        for (int i = 0; i < n; ++i) {
            for (int j = i; j < n; ++j) {
                if (isValid(s, i, j) == true) {
                    if (j - i + 1 > maxStr.length()) { // update maxStr
                        maxStr = s.substring(i, j + 1);
                    }
                }
            }
        }
        return maxStr;
    }

    private boolean isValid(String s, int lo, int hi) {
        int n = hi - lo + 1;
        for (int i = 0; i < n / 2; ++i) {
            if (s.charAt(lo + i) != s.charAt(hi - i)) {
                return false;
            }
        }
        return true;
    }
}
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.