-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution71.cs
39 lines (34 loc) · 904 Bytes
/
Solution71.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Text;
namespace LeetCode.Solutions;
public class Solution71
{
/// <summary>
/// 71. Simplify Path - Medium
/// <a href="https://leetcode.com/problems/simplify-path">See the problem</a>
/// </summary>
public string SimplifyPath(string path)
{
var stack = new Stack<string>();
var parts = path.Split('/');
foreach (var part in parts)
{
if (part == "..")
{
if (stack.Count > 0)
{
stack.Pop();
}
}
else if (part != "." && part != "")
{
stack.Push(part);
}
}
var result = new StringBuilder();
foreach (var part in stack)
{
result.Insert(0, $"/{part}");
}
return result.Length == 0 ? "/" : result.ToString();
}
}