![JAR search and dependency download from the Maven repository](/logo.png)
g1501_1600.s1542_find_longest_awesome_substring.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 g1501_1600.s1542_find_longest_awesome_substring
// #Hard #String #Hash_Table #Bit_Manipulation
// #2023_06_12_Time_239_ms_(100.00%)_Space_38.3_MB_(100.00%)
class Solution {
fun longestAwesome(s: String): Int {
val n = s.length
val idx = IntArray(Math.pow(2.0, 10.0).toInt())
idx.fill(Int.MAX_VALUE)
idx[0] = -1
var mask = 0
var ans = 0
for (i in 0 until n) {
mask = mask xor (1 shl s[i].code - '0'.code)
ans = Math.max(ans, i - idx[mask])
for (j in 0..9) {
ans = Math.max(ans, i - idx[mask xor (1 shl j)])
}
idx[mask] = Math.min(idx[mask], i)
}
return ans
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy