Skip to content

Commit 7d72d80

Browse files
add: Palindrimic substrings
1 parent 110d2e3 commit 7d72d80

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}

0 commit comments

Comments
 (0)