0% found this document useful (0 votes)
6 views3 pages

String

The document provides a collection of Java code solutions for various palindrome problems, including checking if a string is a palindrome, finding the longest palindromic substring, and counting palindromic substrings. It includes techniques such as expanding from the center, using two pointers, and dynamic programming. Additionally, it features a quick memory table summarizing the problems and their corresponding techniques.

Uploaded by

bhavanipriy73
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

String

The document provides a collection of Java code solutions for various palindrome problems, including checking if a string is a palindrome, finding the longest palindromic substring, and counting palindromic substrings. It includes techniques such as expanding from the center, using two pointers, and dynamic programming. Additionally, it features a quick memory table summarizing the problems and their corresponding techniques.

Uploaded by

bhavanipriy73
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

String

03 February 2026 19:00

Substring + symmetry → Expand from center


Substring + uniqueness → Sliding window
Substring + count → HashMap

Perfect
Here is ALL IMPORTANT PALINDROME PROBLEMS — JAVA CODE ONLY, neatly organized.
You can copy–practice–revise. I’ll keep explanations minimal, code clear.

PALINDROME PROBLEMS — JAVA CODE


COLLECTION

public boolean isPalindrome(String s) {


int left = 0, right = [Link]() - 1;
while (left < right) {
if ([Link](left) != [Link](right))
return false;
left++;
right--;
}
return true;
}

public String longestPalindrome(String s) {


if (s == null || [Link]() < 1) return "";
int start = 0, end = 0;
for (int i = 0; i < [Link](); i++) {
int len1 = expand(s, i, i);
int len2 = expand(s, i, i + 1);
int len = [Link](len1, len2);
if (len > end - start) {
start = i - (len - 1) / 2;
end = i + len / 2;
}
}
return [Link](start, end + 1);
}
private int expand(String s, int left, int right) {
while (left >= 0 && right < [Link]()
&& [Link](left) == [Link](right)) {
left--;
right++;
}
return right - left - 1;

string Page 1
return right - left - 1;
}

public int countSubstrings(String s) {


int count = 0;
for (int i = 0; i < [Link](); i++) {
count += expandCount(s, i, i); // odd
count += expandCount(s, i, i + 1); // even
}
return count;
}
private int expandCount(String s, int left, int right) {
int count = 0;
while (left >= 0 && right < [Link]()
&& [Link](left) == [Link](right)) {
count++;
left--;
right++;
}
return count;
}

public boolean validPalindrome(String s) {


int left = 0, right = [Link]() - 1;
while (left < right) {
if ([Link](left) != [Link](right)) {
return isPal(s, left + 1, right)
|| isPal(s, left, right - 1);
}
left++;
right--;
}
return true;
}
private boolean isPal(String s, int l, int r) {
while (l < r) {
if ([Link](l++) != [Link](r--))
return false;
}
return true;
}

public String breakPalindrome(String palindrome) {


if ([Link]() <= 1) return "";
char[] arr = [Link]();
for (int i = 0; i < [Link] / 2; i++) {
if (arr[i] != 'a') {
arr[i] = 'a';
return new String(arr);
}
}
arr[[Link] - 1] = 'b';

string Page 2
arr[[Link] - 1] = 'b';
return new String(arr);
}

public int longestPalindromeSubseq(String s) {


int n = [Link]();
int[][] dp = new int[n][n];
for (int i = n - 1; i >= 0; i--) {
dp[i][i] = 1;
for (int j = i + 1; j < n; j++) {
if ([Link](i) == [Link](j))
dp[i][j] = dp[i + 1][j - 1] + 2;
else
dp[i][j] = [Link](dp[i + 1][j], dp[i][j - 1]);
}
}
return dp[0][n - 1];
}

public String shortestPalindrome(String s) {


int n = [Link]();
int i = 0;
for (int j = n - 1; j >= 0; j--) {
if ([Link](i) == [Link](j))
i++;
}
if (i == n) return s;
String suffix = [Link](i);
return new StringBuilder(suffix).reverse()
+ shortestPalindrome([Link](0, i))
+ suffix;
}

QUICK MEMORY TABLE


Problem Technique
Check palindrome Two pointers
Longest palindromic substring Expand center
Count palindromes Expand center
One deletion allowed Two pointers + skip
Subsequence palindrome DP
Break palindrome Greedy
Shortest palindrome Prefix check

From <[Link]

string Page 3

You might also like