All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2301_2400.s2399_check_distances_between_same_letters.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g2301_2400.s2399_check_distances_between_same_letters

// #Easy #Array #String #Hash_Table #2023_07_02_Time_173_ms_(66.67%)_Space_34.7_MB_(100.00%)

class Solution {
    fun checkDistances(s: String, distance: IntArray): Boolean {
        val seenFirstIndexYet = BooleanArray(26)
        for (idxIntoS in 0 until s.length) {
            val c = s[idxIntoS]
            if (!seenFirstIndexYet[c.code - 'a'.code]) {
                seenFirstIndexYet[c.code - 'a'.code] = true
                distance[c.code - 'a'.code] += idxIntoS
            } else {
                // seenFirstIndexYet[c - 'a']
                distance[c.code - 'a'.code] -= idxIntoS
                if (distance[c.code - 'a'.code] != -1) {
                    // early return
                    return false
                }
            }
        }
        return true
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy