C++ solution with list


#1

#include
string Solution::simplifyPath(string A) {
if (A.size() == 1) return A; //root only

	list<string> paths;
	string current = "";
	for (int i = 1; i < A.size(); ++i) //find dir names
	{
		if (A[i] == '/') //delimiter start new word
		{
			if (current == "..") //erase previous
			{
				if (!paths.empty())
					paths.erase(--paths.end());
			}
			else if (current != ".")
			{
				paths.push_back(current);
			}
			current = "";

		}
		else
		{
			current += A[i];
		}
	}
	// take care of last folder
	if (current == "..")
	{
		if (!paths.empty())
			paths.erase(--paths.end());
	}
	else if (current != ".")
		paths.push_back(current);

	string result = "";
	//join paths
	for (auto s : paths)
	{
		if (s.size() >0)
			result += "/" + s;
	}

	return result.empty() ? "/" : result;

}