[READ-ONLY] Mirror of https://github.com/shuuji3/leetcode. ๐Ÿ“ƒ Code submitted to LeetCode leetcode.com/shuuji3/
leetcode leetcode-solutions
0

Configure Feed

Select the types of activity you want to include in your feed.

Improve is_palindrome() to avoid index access every time.

+5 -6
+5 -6
problems/5.longest-palindromic-substring.py
··· 48 48 j += 1 49 49 50 50 def is_palindrome(self, substring: str) -> bool: 51 - i = 0 52 - j = len(substring) - 1 53 - while i < j: 54 - if substring[i] != substring[j]: 51 + center_index = len(substring) // 2 52 + first_part = substring[:center_index + 1] # 'abc' for 'abcde' 53 + last_part = reversed(substring[center_index:]) # 'edc' for 'abcde' 54 + for c1, c2 in zip(first_part, last_part): 55 + if c1 != c2: 55 56 return False 56 - i += 1 57 - j -= 1 58 57 return True