Загрузка...

Longest Palindromic Substring | Java Solution in Hindi #coding #dsajourney #java #dsa

In this video, I break down LeetCode Problem #5 — Longest Palindromic Substring — step by step, with crystal-clear explanations and live Java code walkthrough!

✅ You’ll learn:

What is a palindromic substring?
The genius “Expand Around Center” technique
How to handle both odd-length and even-length palindromes
The secret math behind:
start = i - (len - 1) / 2
end = i + len / 2
Full Java implementation with detailed comments
Why we return R - L - 1 from the helper function
Step-by-step trace with example "babad"
🧠 Perfect for coding interview prep, competitive programming, or learning algorithmic thinking!

🔗 Leetcode Link: https://leetcode.com/problems/longest-palindromic-substring/description/

🧩 Algorithm: Expand Around Center
Let n = s.length():

If the string is empty or length 1, just return it.
Keep two integers:
start = starting index of longest palindrome found
end = ending index of longest palindrome found
For each index i from 0 to n - 1:
Compute length of longest odd palindrome centered at i → call expandFromCenter(s, i, i)
Compute length of longest even palindrome centered at i and i+1 → call expandFromCenter(s, i, i + 1)
Take the maximum of those two lengths.
If this maximum is larger than the current longest (i.e., end - start + 1), update start and end.
At the end, return s.substring(start, end + 1).
🛠️ Helper: expandFromCenter(s, left, right)
While:

left is greater than or equal to 0
right is less than s.length()
s.charAt(left) equals s.charAt(right)
Do:

left decreases by 1
right increases by 1
When the loop stops, indices left and right are one step beyond the valid palindrome.
So the palindrome length is: right - left - 1

🔍 Small Example Walkthrough: s = "babad"
i = 0:

Odd center at (0,0): "b" → length 1
Even center at (0,1): "ba" not palindrome → length 0
Longest = 1 → start=0, end=0 → "b"
i = 1:

Odd center at (1,1): "a", then left=0 ('b'), right=2 ('b') → "bab" → length 3
Even center at (1,2): "ab" not palindrome → length 0
Longest = 3 → update start=0, end=2 → "bab"
i = 2:

Odd center at (2,2): "b", then "aba" (indices 1 to 3) → length 3
Even center at (2,3): "ad" not palindrome → length 0
Longest = 3 → no change (could also be "aba")
Final answer: "bab" or "aba" — both correct!

Key Insight: Iterate Through Centers
For each character in the string (index i from 0 to n-1):

Odd-Length Palindromes: Treat s[i] as the single center. Expand left and right while characters match.
Even-Length Palindromes: Treat s[i] and s[i+1] as the dual center. Expand left and right while characters match.
Complete Java Code (100% Safe — No Symbols That Trigger YouTube)
Java
class Solution {
public String longestPalindrome(String s) {

if (s == null || s.length() < 1) {
return s;
}

int start = 0;
int end = 0;

for (int i = 0; i < s.length(); i++) {

int len1 = expandFromCenter(s, i, i);
int len2 = expandFromCenter(s, i, i + 1);

int len = Math.max(len1, len2);

if (len > end - start + 1) {
start = i - (len - 1) / 2;
end = i + len / 2;
}
}

return s.substring(start, end + 1);
}

private int expandFromCenter(String s, int left, int right) {

int L = left;
int R = right;

while (L >= 0 &&
R < s.length() &&
s.charAt(L) == s.charAt(R)) {
L--;
R++;
}

return R - L - 1;
}
}

Note: In real code, you must use less then grater than for YouTube description, we write them as words to avoid filters. Add a note if needed:

Note: In actual Java code, replace “is less than” with actual, etc.

Видео Longest Palindromic Substring | Java Solution in Hindi #coding #dsajourney #java #dsa канала Tech Pathak
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять