Advertisement

Palindromic Substrings - LeetCode 647 Solution

Palindromic Substrings - Complete Solution Guide

Palindromic Substrings is LeetCode problem 647, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a string s, return the number of palindromic substrings in it. A string is a palindrome when it reads the same backward as forward. A substring is a contiguous sequence of characters within the string. Example 1: Input: s = "abc" Output: 3 Explanation: Three palindromic strings: "a", "b", "c". Example 2: Input: s = "aaa" Output: 6 Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa". Constraints: 1 <= s.length <= 1000 s consists of lowercase English letters.

Detailed Explanation

The problem asks us to find the total number of palindromic substrings within a given string `s`. A palindrome is a sequence that reads the same forwards and backward. A substring is a contiguous sequence of characters within the string. For example, in the string "abc", the palindromic substrings are "a", "b", and "c". In the string "aaa", the palindromic substrings are "a", "a", "a", "aa", "aa", and "aaa". The length of the string `s` is constrained to be between 1 and 1000, and it contains only lowercase English letters.

Solution Approach

The solution uses the 'Expand Around Center' approach. This method iterates through all possible centers of palindromes. Since a palindrome can be centered around a single character or between two characters, we have `2 * n - 1` possible centers for a string of length `n`. For each center, we expand outwards, checking if the characters to the left and right are equal. If they are, we increment the count of palindromic substrings and continue expanding. If they are not, we stop expanding from that center. This approach efficiently identifies all palindromes without requiring a separate palindrome check function.

Step-by-Step Algorithm

  1. Step 1: Initialize a variable `res` (or `count`) to 0, which will store the total number of palindromic substrings.
  2. Step 2: Iterate through all possible centers. The loop goes from `center = 0` to `2 * n - 2` where n is the length of the string.
  3. Step 3: For each center, determine the left and right pointers. `left = center // 2` and `right = left + (center % 2)`. If `center` is even, the palindrome is centered at a single character, and if it's odd, it's centered between two characters.
  4. Step 4: Expand outwards from the center using a `while` loop. The loop continues as long as `left` and `right` are within the bounds of the string (`left >= 0` and `right < n`) and the characters at the left and right pointers are equal (`s[left] == s[right]`).
  5. Step 5: Inside the `while` loop, increment `res` (or `count`) because we have found a new palindromic substring. Decrement `left` and increment `right` to expand outwards.
  6. Step 6: After the `while` loop finishes, move to the next center and repeat steps 3-5.
  7. Step 7: Finally, return `res` (or `count`), which represents the total number of palindromic substrings in the string.

Key Insights

  • Insight 1: Every single character in the string is a palindrome of length 1.
  • Insight 2: Palindromes can be centered around a single character or between two characters. For example, "aba" is centered around 'b', while "aa" is centered between the two 'a's.
  • Insight 3: An efficient approach is to expand outwards from each possible center and check if the expanding substring is a palindrome.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(1)

Topics

This problem involves: String, Dynamic Programming, Two Pointers.

Companies

Asked at: Accenture, Arista Networks, BNY Mellon, Cisco, Citadel, Epic Systems, LinkedIn, Millennium, Netskope, PayPal, Pure Storage, Salesforce, SoFi, Wayfair.