Skip to content

Instantly share code, notes, and snippets.

@ericness
Created March 31, 2022 00:59
Show Gist options
  • Save ericness/a7175b81a069d5dac9969775eafeaf32 to your computer and use it in GitHub Desktop.
Save ericness/a7175b81a069d5dac9969775eafeaf32 to your computer and use it in GitHub Desktop.
LeetCode 647 optimal solution
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