Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

Corner Cases:


       Did you consider the case where path = "/../"?
       In this case, you should return "/".
       Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
       In this case, you should ignore redundant slashes and return "/home/foo".

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: String algorithms

Data structures

Starter code: class Solution:
    def simplifyPath(self, path: str) -> str:
        

Solutions:
class Solution:

	def simplifyPath(self, path):
		stack = []
		path = [p for p in path.split('/') if p]
		for f in path:
			if f == '.':
				continue
			elif f == '..':
				if stack:
					stack.pop()
			else:
				stack.append(f)
		return '/' + '/'.join(stack)


class Solution:

	def simplifyPath(self, path):
		res = []
		str_list = path.split('/')
		loc = len(str_list) - 1
		for i in range(len(str_list)):
			ss = str_list[i]
			if ss == '':
				continue
			if ss == '.':
				continue
			if ss == '..':
				if len(res) > 0:
					res.pop()
			else:
				res.append(ss)
		return '/' + '/'.join(res)


class Solution:

	def simplifyPath(self, path):
		stack = []
		for p in path.split('/'):
			if p == '..':
				if stack:
					stack.pop()
			elif p and p != '.':
				stack.append(p)
		return '/' + '/'.join(stack)


class Solution:

	def simplifyPath(self, path):
		lst = list([_f for _f in path.split('/') if _f])
		print(lst)
		stack = []
		for item in lst:
			if item == '..':
				if not stack:
					continue
				stack.pop()
			elif item == '.':
				print('here')
				continue
			else:
				stack.append(item)
		return '/' + '/'.join(stack)


class Solution:

	def simplifyPath(self, path):
		place = [p for p in path.split('/') if p != '.' and p != '']
		stack = []
		for p in place:
			if p == '..':
				if len(stack) > 0:
					stack.pop()
			else:
				stack.append(p)
		return '/' + '/'.join(stack)


class Solution:

	def simplifyPath(self, path):
		l = []
		ans = '/'
		names = path.split('/')
		print(('names', names))
		for s in names:
			if s == '..':
				if len(l) != 0:
					l.pop()
			elif s == '.':
				continue
			elif s == '':
				continue
			else:
				l.append(s)
		print(('l: ', l))
		for s in l:
			ans += s + '/'
		n = len(ans)
		if n > 1:
			return ans[:len(ans) - 1]
		else:
			return ans


class Solution:

	def simplifyPath(self, path):
		stack = []
		tmpstr = ''
		path += '/'
		for c in path:
			if c == '/':
				if tmpstr:
					if tmpstr == '..':
						if stack:
							stack.pop()
					elif tmpstr != '.':
						stack.append(tmpstr)
				tmpstr = ''
			else:
				tmpstr += c
		return '/' + '/'.join(stack)


class Solution:

	def simplifyPath(self, path):
		if not path or len(path) == 1:
			return path
		stack = []
		list_path = path.split('/')
		for ch in list_path:
			if ch == '..':
				if stack:
					stack.pop()
			elif ch != '.' and ch:
				stack.append(ch)
		return '/' + '/'.join(stack)


class Solution:

	def simplifyPath(self, path):
		n = len(path)
		if n == 0:
			return path
		v = []
		i = 0
		while i < n:
			while i < n and path[i] == '/':
				i = i + 1
			if i == n:
				break
			left = i
			while i < n and path[i] != '/':
				i = i + 1
			right = i - 1
			s = path[left:right + 1]
			if s == '..':
				if len(v) != 0:
					v.pop()
			elif s != '.':
				v.append(s)
		if len(v) == 0:
			return '/'
		ret = ''
		for i in range(len(v)):
			ret = ret + '/' + v[i]
		return ret


class Solution:

	def simplifyPath(self, path):
		tokens = path.split('/')
		stack = list()
		for token in tokens:
			if token != '':
				stack.append(token)
		res = ''
		back = 0
		while stack:
			top = stack.pop()
			if top == '.':
				continue
			elif top == '..':
				back = back + 1
			elif back == 0:
				res = '/' + top + res
			else:
				back = back - 1
		if res == '':
			return '/'
		return res


class Solution:

	def simplifyPath(self, path):
		places = [tmp for tmp in path.split('/') if tmp != '.' and tmp != '']
		stack = []
		for name in places:
			if name == '.' or name == '':
				continue
			elif name == '..':
				if len(stack) > 0:
					stack.pop()
			else:
				stack.append(name)
		return '/' + '/'.join(stack)


class Solution:

	def simplifyPath(self, path):
		path = path.split('/')
		stack = []
		for n in path:
			if n == '' or n == '.':
				continue
			elif n == '..':
				if stack != []:
					stack.pop()
			else:
				stack.append(n)
		return '/' + '/'.join(stack)