AAAAAAAAAAAAAAAAAAAAAAAAAAA
Code


# Code
```kotlin []
import kotlin.math.abs

class Solution {
    fun maxDistance(s: String, k: Int): Int {
        var result = 0
        var N = 0
        var S = 0
        var W = 0
        var E = 0
        s.forEach {
            when (it) {
                'N' -> N++
                'S' -> S++
                'W' -> W++
                'E' -> E++
            }

            var NS = maxOf(N, S)
            var WE = maxOf(W, E)

            var minNS = minOf(N, S)
            var minWE = minOf(W, E)

            result = maxOf(result, NS + WE - minNS - minWE + 2 * minOf(minNS + minWE, k))
        }
        return result
    }
}
```
['Kotlin']