Leetcode - Palindromic Substrings (Python)

preview_player
Показать описание
March 2021 Leetcode Challenge
Leetcode - Palindromic Substrings #647
Difficulty: Medium
Рекомендации по теме
Комментарии
Автор

Thanks! Python implementation:

class Solution(object):
def countSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
count = 0
for i in range(len(s)):
l = r = i
while l >= 0 and r < len(s) and s[l] == s[r]:
count += 1
l -= 1
r += 1

l = i
r = i + 1
while l >= 0 and r < len(s) and s[l] == s[r]:
count += 1
l -= 1
r += 1
return count

edwardteach
Автор

Nice explanation Tim! Just to help clarify, we do two while loops to include all possible odd and even length sub palindromes. For an odd length palindrome, each char position is the center. For an even length palindrome, each 'pair' is a center.

janmichaelaustria
Автор

What are the time and space complexity?

saketsuraj
Автор

I have also seen this question because I watch you tim. 😆

CEOofTheHood
Автор

hello, what is the space complexity ?

adityachavan
Автор

Don't trust me, I know nothing 😂😂.

archerkol
visit shbcf.ru