Count Palindromic Substrings of Length at Least Two
Updated
Problem statement
Given a string s, count its palindromic substring occurrences whose length is at least 2. A substring is a contiguous interval, and it is palindromic if its characters read the same forward and backward.
Count different index intervals separately, even when they contain the same text. Single-character substrings do not count. For this exercise, s contains only lowercase English letters; an empty string returns 0. Leave s unchanged.
Function
countPalindromicSubstrings(s: String) → intExamples
Example 1
s = "abaab"return = 3The qualifying intervals contain aba at indices 0..2,aa at 2..3,and baab at 1..4. Their lengths are 3,2,4; no singleton is included, so return 3.
Example 2
s = "aaa"return = 3There are two length-2 occurrences aa, at 0..1 and 1..2, plus one length-3 occurrence aaa. They count separately, so return 3 rather than the number of distinct texts.
Constraints
- For this exercise, assume 0 <= s.length <= 500.
- Every character is a lowercase English letter a through z.
More Morgan Stanley Online Assessment questions
- Three-Resource 0/1 KnapsackOA · Seen Sep 2026
- Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
- Maximum Total of Nonadjacent RatingsOA · Seen Jul 2026
- Count Shipment SchedulesOA · Seen Apr 2026
- Delete and EarnOA · Seen Apr 2026
- Interleave Array HalvesOA · Seen Apr 2026
- Get Patch SequenceOA · Seen Feb 2025
- String Subsequences 🐳OA · Seen Mar 2024
- Binary Tree Level OrderOA · Seen Feb 2024
- Merge IntervalsOA · Seen Feb 2024
Similar questions at other companies
- Maximum Equal-Bit Pairs in a SubsequenceAmazon · OA
- Longest Subsequence That Is a SubstringBooking.com · OA
- Count Digit Replacements with No Equal Adjacent DigitsMicrosoft · OA
- Palindrome CounterGoldman Sachs · OA
- Longest String ChainVisa · OA
- Minimum-Cost Digit String DecodingSquadStack.ai · OA
- Maximum Redundancy After One Segment FlipBloomberg LP · OA
- Substrings Without a Target SubsequencePhonePe · OA
- Minimum Edit-Distance String for Each QueryTarget · OA
- Longest Subsequence That Is a SubstringWells Fargo · OA