![JAR search and dependency download from the Maven repository](/logo.png)
g2401_2500.s2484_count_palindromic_subsequences.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 g2401_2500.s2484_count_palindromic_subsequences
// #Hard #String #Dynamic_Programming #2023_07_05_Time_494_ms_(100.00%)_Space_33.9_MB_(100.00%)
class Solution {
fun countPalindromes(s: String): Int {
val n = s.length
var ans: Long = 0
val mod = 1e9.toInt() + 7
val count = IntArray(10)
for (i in 0 until n) {
var m: Long = 0
for (j in n - 1 downTo i + 1) {
if (s[i] == s[j]) {
ans += m * (j - i - 1)
ans = ans % mod
}
m += count[s[j].code - '0'.code].toLong()
}
count[s[i].code - '0'.code]++
}
return ans.toInt()
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy