Given an absolute pathname that may have
.
or ..
as part of it, return the shortest standardized path.
For example, given "/usr/bin/../bin/./scripts/../", return "/usr/bin/".
import java.util.*;
class GFG {
static String getAbsolutePath(String s)
{
String path[]=s.split("/");
ArrayList<String>al=new ArrayList<>();
for(int i=0;i<path.length;i++)
{
if(path[i].equals("."))
continue;
else if(path[i].equals(".."))
{
if(al.size()>0)
al.remove(al.size()-1);
}
else
al.add("/"+path[i]);
}
if(al.size()>0)
al.add("/");
s="";
for(int i=1;i<al.size();i++)
s+=al.get(i);
return s;
}
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.next();
System.out.println(getAbsolutePath(s));
}
}
class GFG {
static String getAbsolutePath(String s)
{
String path[]=s.split("/");
ArrayList<String>al=new ArrayList<>();
for(int i=0;i<path.length;i++)
{
if(path[i].equals("."))
continue;
else if(path[i].equals(".."))
{
if(al.size()>0)
al.remove(al.size()-1);
}
else
al.add("/"+path[i]);
}
if(al.size()>0)
al.add("/");
s="";
for(int i=1;i<al.size();i++)
s+=al.get(i);
return s;
}
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.next();
System.out.println(getAbsolutePath(s));
}
}