@@ -0,0 +1,2155 @@
+package coding4fun;%0A%0Aimport java.util.LinkedList;%0Aimport java.util.stream.Collectors;%0A%0A/**%0A * Find the relative path from a given source to the given destination.%0A * %0A * For simplicity, assume that the file path separator is always /%0A * %0A * Input:%0A *   - Source     : /a/b/c/d/e%0A *   - Destination: /a/b/f/x/y/z%0A * Output: ../../../f/x/y/z%0A * %0A * Input:%0A *   - Source     : /a/b/c/d/e%0A *   - Destination: /a/b/f%0A * Output: ../../../f%0A * %0A * Input:%0A *   - Source     : /a/b/c%0A *   - Destination: /x/y/z%0A * Output: ../../../x/y/z%0A * %0A * Input:%0A *   - Source     : /a/b/c%0A *   - Destination: /a/b/c%0A * Output: c%0A */%0Apublic class Problem20 %7B%0A    public static String getRelativePath(String src, String dest) %7B%0A        String%5B%5D splitSrc = src.split(%22/%22);%0A        LinkedList%3CString%3E srcPath = new LinkedList%3C%3E();%0A        for (String path : splitSrc) %7B%0A            srcPath.add(path);%0A        %7D%0A        String%5B%5D splitDest = dest.split(%22/%22);%0A        LinkedList%3CString%3E destPath = new LinkedList%3C%3E();%0A        for (String path : splitDest) %7B%0A            destPath.add(path);%0A        %7D%0A        if (src.equals(dest)) %7B%0A            return destPath.peekLast();%0A        %7D%0A        String p1 = srcPath.peekFirst();%0A        String p2 = destPath.peekFirst();%0A        while (p1.equals(p2)) %7B%0A            srcPath.removeFirst();%0A            destPath.removeFirst();%0A            %0A            if (srcPath.isEmpty() %7C%7C destPath.isEmpty()) %7B%0A                break;%0A            %7D%0A            p1 = srcPath.peekFirst();%0A            p2 = destPath.peekFirst();%0A        %7D%0A        StringBuilder relativePath = new StringBuilder();%0A        for (int i = 0; i %3C srcPath.size(); i++) %7B%0A            relativePath.append(%22../%22);%0A        %7D%0A        relativePath.append(destPath.stream().collect(Collectors.joining(%22/%22)));%0A        return relativePath.toString();%0A    %7D%0A    %0A    public static void main(String%5B%5D args) %7B%0A        System.out.println(getRelativePath(%22/a/b/c/d/e%22, %22/a/b/f/x/y/z%22));%0A        System.out.println(getRelativePath(%22/a/b/c/d/e%22, %22/a/b/f%22));%0A        System.out.println(getRelativePath(%22/a/b/c%22, %22/x/y/z%22));%0A        System.out.println(getRelativePath(%22/a/b/c%22, %22/a/b/c%22));%0A    %7D%0A%7D%0A

Add problem 20