All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : February 04, 2025
Last updated : February 04, 2025
Related Topics : Hash Table, Math, String, Counting
Acceptance Rate : 30.85 %
class Solution:
def maxDistance(self, s: str, k: int) -> int:
N, S, E, W = 0, 0, 0, 0
output_max = 0
for c in s :
match c :
case 'N' :
N += 1
case 'S' :
S += 1
case 'E' :
E += 1
case 'W' :
W += 1
case _ :
pass
output_max = max(output_max, abs(N - S) + abs(E - W) + 2 * (min(k, min(N, S) + min(E, W))))
return output_max