![JAR search and dependency download from the Maven repository](/logo.png)
g2501_2600.s2564_substring_xor_queries.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 g2501_2600.s2564_substring_xor_queries
// #Medium #Array #String #Hash_Table #Bit_Manipulation
// #2023_07_08_Time_1603_ms_(100.00%)_Space_129.9_MB_(100.00%)
class Solution {
fun substringXorQueries(s: String, queries: Array): Array {
val n = s.length
val indices = queries.withIndex().groupBy(
keySelector = { it.value[0] xor it.value[1] },
valueTransform = { it.index }
).toMutableMap()
val res = Array(queries.size) { IntArray(2) { -1 } }
fun helper(value: Int, left: Int, right: Int) {
(indices.remove(value) ?: return).forEach {
res[it][0] = left
res[it][1] = right
}
}
for (i in 0 until n) {
if (s[i] == '0') {
helper(0, i, i)
} else {
var tmp = 0L
for (j in i until n) {
tmp = (tmp shl 1) + (s[j] - '0')
if (tmp > Int.MAX_VALUE) break
helper(tmp.toInt(), i, j)
}
}
}
return res
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy