Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2078b5b

Browse files
committed
Solve 131. Palindrome Partitioning
1 parent a7b297c commit 2078b5b

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

‎01/0131-Palindrome_Partitioning.kt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution {
2+
3+
private fun isPalindrome(s: String): Boolean {
4+
var left = 0
5+
var right = s.length - 1
6+
7+
while (true) {
8+
if (s[left] != s[right]) return false
9+
if (left in right..(right + 1)) break
10+
11+
left++
12+
right--
13+
}
14+
15+
return true
16+
}
17+
18+
private fun partition(current: Int, s: String, partition: MutableList<String>, partitions: MutableList<List<String>>) {
19+
if (current == s.length) {
20+
partitions.add(partition.toList())
21+
return
22+
}
23+
24+
var currentString = ""
25+
26+
for (i in current until s.length) {
27+
currentString += s[i]
28+
29+
if (isPalindrome(currentString)) {
30+
partition.add(currentString)
31+
partition(i + 1, s, partition, partitions)
32+
partition.removeAt(partition.size - 1)
33+
}
34+
}
35+
}
36+
37+
fun partition(s: String): List<List<String>> {
38+
val partition = mutableListOf<String>()
39+
val partitions = mutableListOf<List<String>>()
40+
41+
partition(0, s, partition, partitions)
42+
43+
return partitions
44+
}
45+
46+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /