Java Solution with Lambda


  • 0
    M
    class Solution {
        public String simplifyPath(String path) {
            List<String> simplePath = new LinkedList<>();
            String[] ss = path.split("/+");
            for (String s : ss) {
                if ("..".equals(s) && !simplePath.isEmpty()) {
                    simplePath.remove(simplePath.size()-1);
                } else if (!".".equals(s) && !"..".equals(s)) {
                    simplePath.add(s);
                }
            }
    
            return simplePath.stream()
                    .filter((a)->!a.isEmpty())
                    .map((a) -> "/"+a)
                    .reduce((a,b)->a+b)
                    .orElse("/");
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.