Skip to content

Practice Maximum Manhattan Distance After K Changes

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3443
LeetCode Problem

Maximum Manhattan Distance After K Changes

Problem Statement

Starting at (0, 0), follow s in order, where N, S, E, and W each move one unit in the named direction. You may replace at most k characters with any of those directions. Return the greatest Manhattan distance |x| + |y| from the origin attainable at any point during the modified walk, not only at its end.

Source: Maximum Manhattan Distance After K Changes on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "NWSE", k = 1
Output
3

# Example 2

Input
s = "NSWWEW", k = 3
Output
6

Constraints

1 <= s.length <= 10^5
0 <= k <= s.length
s consists of only 'N', 'S', 'E', and 'W'.

Related LeetCode Problems

Practice more problems similar to Maximum Manhattan Distance After K Changes.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.