g1901_2000.s1980_find_unique_binary_string.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 g1901_2000.s1980_find_unique_binary_string
// #Medium #Array #String #Backtracking #2023_06_21_Time_186_ms_(50.00%)_Space_37.5_MB_(66.67%)
class Solution {
fun findDifferentBinaryString(nums: Array): String {
val set: Set = HashSet(listOf(*nums))
val len = nums[0].length
val sb = StringBuilder()
var i = 0
while (i < len) {
sb.append(1)
i++
}
val max = sb.toString().toInt(2)
for (num in 0..max) {
var binary = Integer.toBinaryString(num)
if (binary.length < len) {
sb.setLength(0)
sb.append(binary)
while (sb.length < len) {
sb.insert(0, "0")
}
binary = sb.toString()
}
if (!set.contains(binary)) {
return binary
}
}
return ""
}
}