Created
March 31, 2022 00:59
-
-
Save ericness/a7175b81a069d5dac9969775eafeaf32 to your computer and use it in GitHub Desktop.
LeetCode 647 optimal solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def countSubstrings(self, s: str) -> int: | |
"""Find number of palindromic strings in s | |
Args: | |
s (str): String to analyze | |
Returns: | |
int: Count of palindromes | |
""" | |
palindromes = 0 | |
for i in range(len(s)): | |
left, right = i, i | |
while left >= 0 and right < len(s) and s[left] == s[right]: | |
palindromes += 1 | |
left -= 1 | |
right += 1 | |
left, right = i, i + 1 | |
while left >= 0 and right < len(s) and s[left] == s[right]: | |
palindromes += 1 | |
left -= 1 | |
right += 1 | |
return palindromes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment