File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ '''
2+ ๋ฐฉ์ : Brute-Force
3+ ๋ฌธ์์ two-pointer๋ฅผ ์ง์ ํด์ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ํ์ธํ๊ณ
4+ palindrome์ ์๋ฅผ ๋ฐํ
5+
6+ Time Complexity : O(N^3)
7+ - outer loop(For๋ฌธ) : O(N)
8+ - inner loop(While๋ฌธ) : O(N)
9+ - palindrome ํ์ธ์ ์ํด s[start:end] ์ฌ๋ผ์ด์ฑ ๋ฐ ์ญ์ ํ์ธ : O(N)
10+
11+ Space Complexity: O(N)
12+ - substring์ ์์ ์ ์ฅํ๋๋ฐ ๋๋ ๊ณต๊ฐ ๋น์ฉ
13+ '''
14+
15+ class Solution :
16+ def countSubstrings (self , s : str ) -> int :
17+ cnt = 0
18+ n = len (s )
19+
20+ for start in range (n ):
21+ end = n
22+ while start < end :
23+ sub = s [start :end ]
24+ if sub == sub [::- 1 ]:
25+ cnt += 1
26+ end -= 1
27+
28+ return cnt
29+
30+ '''
31+ ์ ํ์ด๋ก ํด๊ฒฐํ์ง๋ง, ์๊ฐ๋ณต์ก๋๊ฐ ๋ง์ด ๋๋ ๊ฒ์ ํ์
ํ
32+ ์๊ณ ๋ฌ๋ ํ์ด 3: Two Pointers ์ดํดํ๊ณ ์์ฑํ ์ฝ๋
33+
34+ while ๋ฌธ์ 2ํ๋ก ๋๋ ์ด์ ๋ aa, baab ํ์์ ์ง์ palindrome์ ํ์
ํ๊ธฐ ์ํจ
35+ ์ง์ palindrome์ ๊ฒฝ์ฐ ์ ์์ ๊ฐ์ด ๋์ผํด์ผ ๊ทธ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ข์ฐ ๊ฐ์ ๋น๊ตํด์ ํ๋จํ ์ ์์
36+ '''
37+ def countSubstrings (s : str ) -> int :
38+ cnt = 0
39+
40+ for i in range (0 , len (s )):
41+ start , end = i ,i
42+ while start >= 0 and end < len (s ) and s [start ] == s [end ]:
43+ cnt += 1
44+ start -= 1
45+ end += 1
46+
47+ start , end = i , i + 1
48+ while start >= 0 and end < len (s ) and s [start ] == s [end ]:
49+ cnt += 1
50+ start -= 1
51+ end += 1
52+
53+ return cnt
You canโt perform that action at this time.
0 commit comments