We want to find the longest palindromic substring, where a substring implies that all characters have to be consecutive, and palindromic implies that the string is mirrored from the center.
Well, there are multiple ways, one is to iterate over all substrings in the string s and check if it’s a palindrome. But that’s not very good runtime, it’s , for small inputs it’s OK but not for larger ones.
A more efficient technique is to iterate over the string one char at a time and expand from the center to check if the current window of characters is a palindrome.
Code:
Java
Big O Analysis
-
Runtime
The runtime complexity here is .
-
Memory
The memory usage is since we don’t use any datastructure.
— A