Skip to content

Latest commit

 

History

History
14 lines (12 loc) · 560 Bytes

File metadata and controls

14 lines (12 loc) · 560 Bytes

Hash Table

We add all substrings of length k into a hash set. If the size of the hash set is equal to 2^k, we return true. Otherwise, we return false.

fun hasAllCodes(s: String, k: Int): Boolean {
    val substrings = HashSet<String>()
    for (i in 0..s.length - k) {
        substrings.add(s.substring(i, i + k))
    }
    return substrings.size == 2.0.pow(k).toInt()
}