We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d5abaa4 commit 7ec0dd8Copy full SHA for 7ec0dd8
1 file changed
palindromic-substrings/HYUNAHKO.py
@@ -0,0 +1,21 @@
1
+class Solution:
2
+ def countSubstrings(self, s: str) -> int:
3
+ self.count = 0
4
+
5
+ # 중심(left, right)에서 시작해서 양옆으로 퍼지며 팰린드롬 찾기
6
+ def expand(left: int, right: int):
7
+ # 인덱스 범위를 벗어나지 않고 양쪽 문자가 같으면 팰린드롬 발견
8
+ while left >= 0 and right < len(s) and s[left] == s[right]:
9
+ self.count += 1 # 미츠케타!!!
10
+ left -= 1 # 왼쪽으로 한 칸 확장
11
+ right += 1 # 오른쪽으로 한 칸 확장
12
13
+ for i in range(len(s)):
14
+ # 홀수 길이 (중심이 i 하나) -> ex."aba"의 'b'
15
+ expand(i, i)
16
17
+ # 짝수 길이 (중심이 i와 i+1 사이) -> ex."abba"의 'bb'
18
+ expand(i, i + 1)
19
20
+ return self.count
21
0 commit comments