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

g1401_1500.s1496_path_crossing.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.28
Show newest version
package g1401_1500.s1496_path_crossing

// #Easy #String #Hash_Table #2023_06_13_Time_120_ms_(100.00%)_Space_34.9_MB_(93.33%)

class Solution {
    fun isPathCrossing(path: String): Boolean {
        val visited = ArrayDeque()
        visited.add(Coord(0, 0))
        for (c in path.toCharArray()) {
            val last = visited.last()
            if (c == 'N') {
                val nextStep = Coord(last.x, last.y + 1)
                if (visited.contains(nextStep)) {
                    return true
                }
                visited.add(nextStep)
            } else if (c == 'S') {
                val nextStep = Coord(last.x, last.y - 1)
                if (visited.contains(nextStep)) {
                    return true
                }
                visited.add(nextStep)
            } else if (c == 'E') {
                val nextStep = Coord(last.x - 1, last.y)
                if (visited.contains(nextStep)) {
                    return true
                }
                visited.add(nextStep)
            } else if (c == 'W') {
                val nextStep = Coord(last.x + 1, last.y)
                if (visited.contains(nextStep)) {
                    return true
                }
                visited.add(nextStep)
            }
        }
        return false
    }

    internal data class Coord(var x: Int, var y: Int)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy