← 返回 microsoft 的题目列表Optimize Path by Canceling Opposite Directions
类型:online_judge
microsoft
Path Optimization Problem
You plan to visit a friend who lives far away. You will walk the entire journey, back and forth twice. You have an initial path with north (N), south (S), east (E), and west (W) characters. You want to find the shortest version of this path such that the start and end points remain the same. It is guaranteed that for every direction, there is an opposite direction.
Input
forth: A string representing the initial path, comprised of 'N', 'E', 'W', 'S' characters, with a length within [3, 1000].
Output
Return a string representing the shortened path.
Example
Input: forth = "NEENWN" Output: "WSSE"
Input: forth = "NWNENWN" Output: "ESSSSE"
Input: forth = "NENENWWN" Output: "WSSSSEE"
Example
Input
NEENWN