Description
Solutions
Palindrome Counter
🤘 INTERN🔥 FULLTIME📚RELATED PROBLEMS
A palindrome is a string that reads the same from the left and from the right. For example, mom
and
tacocat
are palindromes, as are any single-character strings. Given a string, determine the number
of its substrings that are palindromes.
Function Description
Complete the function countPalindromes
in the editor.
countPalindromes
has the following parameter:
string s
: the string to analyze
Returns
int: an integer that represents the number of palindromic substrings in the given string
Example 1:
Input: s = "tacocat"
Output: 10
Explanation:Palindromic substrings are ['t', 'a', 'c', 'o', 'c', 'a', 't', 'coc', 'acoca', 'tacocat']. There are 10 palindromic substrings.
Example 2:
Input: s = "aaa"
Output: 6
Explanation:There are 6 possible substrings of s: {'a', 'a', 'a', 'aa', 'aa', 'aaa'}. All of them are palindromes, so return 6.
Example 3:
Input: s = "abccba"
Output: 9
Explanation:There are 21 possible substrings of s, the following 9 of which are palindromes: {'a', 'a', 'b', 'b', 'c', 'c', 'cc', 'bccb', 'abccba'}.
Example 4:
Input: s = "daata"
Output: 7
Explanation:There are 15 possible substrings of s, the following 7 of which are palindromes: {'a', 'a', 'a', 'aa', 'ata', 'd', 't'}.
Constraints:
1 ≤ |s| ≤ 5 x 103
each character of s, s[i] ∈ ['a'-'z'].
Related Problems
Testcase
Result
Case 1
input:
output: