Skip to content

Commit 4d7879e

Browse files
week11 mission palindromic-substrings
1 parent 9d339f3 commit 4d7879e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/palindromic-substrings/
2+
- ํ’€์ด: https://algorithm.jonghoonpark.com/2024/07/08/leetcode-647
3+
4+
```java
5+
class Solution {
6+
public int countSubstrings(String s) {
7+
int count = 0;
8+
9+
char[] charArray = s.toCharArray();
10+
for (int i = 0; i < s.length(); i++) {
11+
char currentChar = charArray[i];
12+
count++;
13+
14+
int left = i;
15+
int right = i;
16+
17+
while (right < s.length() - 1 && currentChar == charArray[right + 1]) {
18+
right++;
19+
count++;
20+
}
21+
22+
while (left > 0 && right < s.length() - 1 && charArray[left - 1] == charArray[right + 1]) {
23+
left--;
24+
right++;
25+
count++;
26+
}
27+
}
28+
29+
return count;
30+
}
31+
}
32+
```
33+
34+
### TC, SC
35+
36+
์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ํ‰๊ท ์ ์œผ๋กœ O(n)์ด๋‹ค. palindrome ์˜ ๊ธธ์ด๊ฐ€ n ์— ๊ฐ€๊นŒ์›Œ์งˆ์ˆ˜๋ก ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n^2) ์— ๊ฐ€๊นŒ์›Œ ์ง„๋‹ค.
37+
๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์ด๋‹ค.

0 commit comments

Comments
ย (0)