Grokking Data Structures & Algorithms for Coding Interviews
Vote

0% completed

​

Solution: Simplify Path

.....

.....

.....

Like the course? Get enrolled and start learning!
A

Anonymous

· 2 years ago

class Solution:     def simplifyPath(self, path):                 stack = []         i = 0         temp = []  # for accumulating sub path characters like "foo/home". it stores foo and home         while i < len(path):             # if we have perviously accumulated path push it to the stack             if path[i] == "/" and len(temp) > 0:                 stack.append(''.join(temp))                 temp = []             # ".." case. pop the previous element             elif path[i] == "." and len(stack) > 0 and stack[-1] == '.':                 stack.pop()                 if len(stack) > 0:                     stack.pop()             # "/." case. pop the "."             elif path[i] == "/" and len(stack) > 0 and stack[-1] == '.':                 stack.pop()             #
A

Anonymous

· 3 years ago

So I ended up with that awful, yet working solution

class Solution:     def simplifyPath(self, path):         stack = []         l = len(path)         i = 0         while i < l:             char = path[i]             if char == '/':                 while stack and stack[-1] == '/':                     stack.pop()             elif char == '.':                 if i < l - 1 and path[i + 1] == '.':                     if stack and stack[-1] == '/':                         stack.pop()                     while stack and stack[-1] != '/':                         stack.pop()             if char != '.':                 stack.append(char)             i += 1         while stack and stack[-1] == '/':             stack.pop()         if not stack:             return '/'         re
A

Anonymous

· 3 years ago

In my view, even in Java, as for Python (where you have syntactic sugar like list comprehensions and tuple unpacking for example), the solution should leverage the language specific API.

In modern Java we don't use Stack anymore, so ArrayDeque should be used. In Java ArrayDeque is a double-ended queue that can be used to add elements to the head or to the tail and remove elements from the head and from the tail. Unlike LinkedList, ArrayDeque guarantees to retrieve the elements in the same order as they were entered when using an iterator (for loop).

import java.util.*; import java.util.stream.*; public class Solution { public String simplifyPath(String path) { Deque<String> stack = new ArrayDeque<>(); var patWoSlash = path.split("/");
A

Anonymous

· 3 years ago

  • The stack is processed in LIFO (Last-In-First-Out) order. Each component is popped and appended to the start of the result string, preceded by "/".

This is a bit wrong.

The stack contains [a, b, c]. Using LIFO will create a string: /c/b/a

It should be noted that for-each uses internal array-based representation.

Show 2 replies
A

Anonymous

· 3 years ago

return join statement uses stack as array ....

Reading Progress

0%


Vote for new content