commit 561d8cfd2522570c9953965b97b4da4e25841f49 parent af5b8eae8f485b6d2e43ee4dbc3438a5800fdcbb Author: Dimitrije Dobrota <mail@dimitrijedobrota.com> Date: Mon, 10 Apr 2023 13:57:14 +0200 Random Problem Diffstat:
A | Problems/0071.cpp | | | 26 | ++++++++++++++++++++++++++ |
M | README.md | | | 1 | + |
2 files changed, 27 insertions(+), 0 deletions(-)
diff --git a/Problems/0071.cpp b/Problems/0071.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + string simplifyPath(string path) { + deque<string> dq; + int start = 0; + + path.push_back('/'); + for(int i=0; i<path.size(); i++) { + if(path[i] != '/') continue; + else { + string s = path.substr(start, i - start); + if(s=="..") { + if(!dq.empty()) dq.pop_back(); + }else if(!s.empty() && s!=".") dq.push_back(s); + start = i + 1; + } + } + + string res = ""; + while(!dq.empty()) { + res+="/" + dq.front(); + dq.pop_front(); + } + return res.empty() ? "/" : res; + } +}; diff --git a/README.md b/README.md @@ -88,6 +88,7 @@ for solving problems. | 0068 | Hard | [Text Justification](Problems/0068.cpp) | | 0069 | Easy | [Sqrt(x)](Problems/0069.cpp) | | 0070 | Easy | [Climbing Stairs](Problems/0070.cpp) | +| 0071 | Medium | [Simplify Path](Problems/0071.cpp) | | 0072 | Hard | [Edit Distance](Problems/0072.cpp) | | 0074 | Medium | [Search a 2D Matrix](Problems/0074.cpp) | | 0075 | Medium | [Sort Colors](Problems/0075.cpp) |