Skip to content

Commit 6bbe081

Browse files
committed
feat: [Week 04-4] solve palindromic-substrings
1 parent 645e529 commit 6bbe081

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Solution:
3+
1) ์ž์‹ ์„ ๊ธฐ์ค€์œผ๋กœ l,r ํฌ์ธํ„ฐ๋กœ ๋Š˜๋ ค์ฃผ๋ฉด์„œ ๊ฐ™์€ ๋ฌธ์ž์ด๋ฉด palindrome
4+
์ด๋ฅผ ํ™€์ˆ˜, ์ง์ˆ˜ ๊ธ€์ž์— ๋Œ€ํ•ด 2๋ฒˆ ์ง„ํ–‰ํ•ด์ฃผ๋ฉด๋œ๋‹ค.
5+
Time: O(n^2) = O(n) * O(n/2 * 2)
6+
Space: O(1)
7+
8+
"""
9+
10+
11+
class Solution:
12+
def countSubstrings(self, s: str) -> int:
13+
result = 0
14+
for i in range(len(s)):
15+
l, r = i, i
16+
while l >= 0 and r < len(s):
17+
if s[l] != s[r]:
18+
break
19+
l -= 1
20+
r += 1
21+
result += 1
22+
23+
l, r = i, i + 1
24+
while l >= 0 and r < len(s):
25+
if s[l] != s[r]:
26+
break
27+
l -= 1
28+
r += 1
29+
result += 1
30+
return result

0 commit comments

Comments
ย (0)