File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ // tc O(n^3)... 입력값이 조금만 켜졌으면 바로 TLE가 발생했을 것..
2+ class Solution {
3+ public int countSubstrings (String s ) {
4+ int pCnt = 0 ;
5+
6+ for (int i = 1 ; i <= s .length (); i ++) {
7+ String window = s .substring (0 , i );
8+ String prev = window ;
9+
10+ if (isPalindrome (window ))
11+ pCnt ++;
12+
13+ for (int j = window .length (); j < s .length (); j ++) {
14+ prev = new StringBuilder (prev ).append (s .charAt (j )).substring (1 );
15+ if (isPalindrome (prev )) {
16+ pCnt ++;
17+ }
18+ }
19+ }
20+
21+ return pCnt ;
22+ }
23+
24+ public boolean isPalindrome (String p ) {
25+
26+ if (p .length () <= 0 )
27+ return false ;
28+
29+ int start = 0 ;
30+ int end = p .length () - 1 ;
31+
32+ while (start <= end ) {
33+ if (p .charAt (start ) != p .charAt (end )) {
34+ return false ;
35+ }
36+
37+ start ++;
38+ end --;
39+ }
40+
41+ return true ;
42+ }
43+ }
You can’t perform that action at this time.
0 commit comments