g1401_1500.s1496_path_crossing.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 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)
}