Skip to content

Commit 824a904

Browse files
committed
[Week4](gmlwls96) Palindromic-substring
1 parent f273ad4 commit 824a904

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

palindromic-substrings/gmlwls96.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
// 알고리즘 : brute-force
3+
/** 풀이
4+
* 1. 모든 substring을 전부 뽑아낸다.
5+
* 2. 해당 substring이 palindrome인지 체크한다.
6+
*/
7+
// 시간 : O(n^3)
8+
fun countSubstrings(s: String): Int {
9+
var count = 0
10+
for (len in 1..s.length) { // 길이
11+
for (i in 0..(s.length - len)) { // i : sub string start index.
12+
if (checkPalindrome(s.substring(i, i + len))) {
13+
count++
14+
}
15+
}
16+
}
17+
return count
18+
}
19+
20+
private fun checkPalindrome(subStr: String): Boolean {
21+
return if (subStr.length == 1) {
22+
true
23+
} else {
24+
val reverse = subStr.reversed()
25+
subStr == reverse
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)