5. Longest Palindromic Substring 🔥

Medium

Problem:

Find the longest palindromic substring.

Input: s = "babad" 
Output: "bab" 
Explanation: "aba" is also a valid answer.

https://leetcode.com/problems/group-anagrams/arrow-up-right

What to learn:

Two Pointers

It primarily targets sorted arrays, and solves problems by freely moving two pointers from left to right.

Sliding Window

It can also be applied to unsorted arrays. The window size is fixed and only moves in the left or right direction.

Name
Sorted
Window size
Move

Two pointers

Most O

Variable

Bidirectional left-right

Sliding window

X

Fixed

Unidirectional left or right

Solutions:

Another approach: a solution that expands from the center

  1. Two-pointers, consisting of 2 spaces(even-length palindrome) and 3 spaces(odd-length palindrome), move from the beginning to the end of the string like a sliding window.

  2. When the string that comes into the window is a palindrome, it stops at that spot and extends the two pointers.

  3. Handle the exception.

chevron-rightNested Functionhashtag

It's not often used from work, but it's a very frequently used feature in coding tests where solutions often need to be achieved with a single function.

  • It can freely read the local variables of the parent function, so there's no need to pass parameters each time.

  • If it's a mutable object, it can be manipulated by various operations. Immutable objects like strings cannot be manipulated.

  • If reassignment occurs, the reference ID changes and it is declared as a separate local variable. In other words, the reassigned value (modified value) is not reflected in the parent function.

Operator manipulation

Reassignment

Last updated