![JAR search and dependency download from the Maven repository](/logo.png)
g1101_1200.s1147_longest_chunked_palindrome_decomposition.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g1101_1200.s1147_longest_chunked_palindrome_decomposition
// #Hard #String #Dynamic_Programming #Greedy #Two_Pointers #Hash_Function #Rolling_Hash
// #2023_05_31_Time_148_ms_(50.00%)_Space_38.2_MB_(50.00%)
class Solution {
fun longestDecomposition(text: String): Int {
val n = text.length
var l = 0
var r = n - 1
var len = 1
var ans = 0
var lft: String
var rit: String
var perfectSubstring = false
while (l + len <= r - len + 1) {
lft = text.substring(l, l + len)
rit = text.substring(r - len + 1, r + 1)
if (lft == rit) {
ans += 2
if (l + len == r - len + 1) {
perfectSubstring = true
break
}
l = l + len
r = r - len
len = 1
} else {
len++
}
}
if (!perfectSubstring) {
ans++
}
return ans
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy