File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 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)์ด๋ค.
You canโt perform that action at this time.
0 commit comments