Problem Statement#
Given a string find all non-single letter substrings that are palindromes. For instance:
Solution#
Solution Explanation#
Runtime complexity#
The runtime complexity of this solution is polynomial, O(n2)O(n2)
Memory complexity#
The memory complexity of this solution is constant, O(1).
Solution Breakdown#
For each letter in the input string, start expanding to the left and right while checking for even and odd length palindromes. Move to the next letter if we know a palindrome doesn’t exist. We expand one character to the left and right and compare them. If both of them are equal, we print out the palindrome substring.
Practice problems like this and many more by checking out our Grokking the Coding Interview: Patterns for Coding Questions course!
Free Resources