Skip to content

Commit 7ec0dd8

Browse files
committed
week 08 palindromic-substrings solution
1 parent d5abaa4 commit 7ec0dd8

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

palindromic-substrings/HYUNAHKO.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)