/*
 * Copyright (C) 2014 Pedro Vicente Gómez Sánchez.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.pedrovgs.problem37;

/**
 * Given an absolute path and two relative paths to a directory, can you write a method to calculate
 * the final path?
 *
 * @author Pedro Vicente Gómez Sánchez.
 */
public class PathCalculator {
}

@@ -645,16 +645,69 @@
 lem37;%0A%0A
+import java.util.LinkedList;%0Aimport java.util.List;%0A%0A
 /**%0A * G
@@ -792,16 +792,19 @@
 ethod to
+%0A *
  calcula
@@ -901,11 +901,1484 @@
 ulator %7B
+%0A%0A  public static final String DIR_SEPARATOR = %22/%22;%0A%0A  /**%0A   * Iterative algorithm to solve this problem. The complexity order of this algorithm is O(N+M+%C3%91)%0A   * where N,M and %C3%91 are the length of every path. We are going to transform string path to arrays%0A   * and evaluate every one in a different way adding and subtracting elements from a list.%0A   */%0A  public String calculate(String absolute, String r1, String r2) %7B%0A    if (absolute == null %7C%7C r1 == null %7C%7C r2 == null) %7B%0A      throw new IllegalArgumentException(%22You can't pass null strings as input.%22);%0A    %7D%0A%0A    String%5B%5D absolutePath = absolute.split(DIR_SEPARATOR);%0A    String%5B%5D r1Path = r1.split(DIR_SEPARATOR);%0A    String%5B%5D r2Path = r2.split(DIR_SEPARATOR);%0A    String%5B%5D result = calculatePath(absolutePath, r1Path, r2Path);%0A%0A    StringBuilder stringBuilder = new StringBuilder(DIR_SEPARATOR);%0A    for (String dir : result) %7B%0A      stringBuilder.append(dir);%0A      stringBuilder.append(%22/%22);%0A    %7D%0A    return stringBuilder.toString();%0A  %7D%0A%0A  private static String%5B%5D calculatePath(String%5B%5D... paths) %7B%0A    List%3CString%3E finalPath = new LinkedList%3CString%3E();%0A    for (String%5B%5D path : paths) %7B%0A      for (String dir : path) %7B%0A        if (dir.equals(%22..%22)) %7B%0A          if (!finalPath.isEmpty()) %7B%0A            finalPath.remove(finalPath.size() - 1);%0A          %7D%0A        %7D else if (!dir.isEmpty()) %7B%0A          finalPath.add(dir);%0A        %7D%0A      %7D%0A    %7D%0A    return finalPath.toArray(new String%5BfinalPath.size()%5D);%0A  %7D
 %0A%7D%0A

Add iterative solution to problem 37