Longest Absolute File Path

A couple strange things are happening to me with this challenge. If you wish to take a look at the requirements and provide some insights you can find it at:  https://leetcode.com/problems/longest-absolute-file-path/?tab=Description

As you can see there are a couple of sample strings. They are:

dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext

dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext

Allow me to show a screen capture of the Eclipse IDE console:

main <<< java.runtime.version ==>1.8.0_121-b13<==

dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext

dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext

dir\n\tsubdir1\n\tsubdir2\n\tsubdir3

aaaaaaaaaaaaaaaaaaaaa\n\tsth.png

a\n\taa\n\t\taaa\n\t\t\tfile1.txt

-1

main <<< fs ==>dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext<==

main <<< length: 20

main <<< fs ==>dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext<==

main <<< length: 32

main <<< fs ==>dir\n\tsubdir1\n\tsubdir2\n\tsubdir3<==

main <<< length: 0

main <<< fs ==>aaaaaaaaaaaaaaaaaaaaa\n\tsth.png<==

main <<< length: 29

main <<< fs ==>a\n\taa\n\t\taaa\n\t\t\tfile1.txt<==

main <<< length: 18

main <<< fs ==>-1<==

As usual, I wrote a test program and from it I built the solution. I always like to use a TDD approach.

The source code for main() in Java follows:

/**

* Test code.

*/

public static void main(String[] args) {

// **** display Java runtime version ****

System.out.println(“main <<< java.runtime.version ==>” + System.getProperty(“java.runtime.version”) + “<==”);

// **** ****

String fs;

// **** open scanner ****

Scanner sc = new Scanner(System.in);

do {

// **** read file system ****

fs = sc.next();

System.out.println(“main <<< fs ==>” + fs + “<==”);

// **** length of the longest absolute path to file ****

if (!fs.equals(“-1”)) {

System.out.println(“main <<< length: ” + lengthLongestPath(fs));

}

} while (!fs.equals(“-1”));

// **** close scanner ****

sc.close();

}

The first lines display the version of Java I am using. If there is an issue or changes that might impact what I am observing please let me now.

As you can tell, I input most of the paths discussed in the LeetCode challenge. Of interest is the first one.

It seems to me that my code might have lots of issues but seems to work with the sample first line. After copying the code to the web site and clicking on the <Run Code> button (repeated with minor modifications a dozen or so times) I get the following:

Your input

“dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext”

Your answer

32

Expected answer

20

Seems to me that the first path I am using for testing matches what is on the requirements section at the web site and matches the test case that failed :o(

Please take a look at my Java code which is working on my computer but failing at LeetCode:

/**

* Determine if this path is a directory.

*/

static boolean isDirectory(String path) {

return !path.contains(“.”);

}

/**

* Determine the directory level.

*/

static int dirLevel(String name) {

// ???? strange behaviour ????

String delim = “\\t”;

String[] split = name.split(Pattern.quote(delim));

// **** ****

return split.length – 1;

}

/**

* Build full path.

*/

static String buildFullPath(Stack<String> pathStack, String fileName) {

String fullPath = “”;

for (int i = 0; i < pathStack.size(); i++) {

fullPath += pathStack.elementAt(i);

fullPath += “/”;

}

return fullPath += fileName;

}

/**

* An implementation.

*/

static int lengthLongestPath(String input) {

int maxLength              = 0;

int currentLevel           = -1;

Stack<String> pathStack    = new Stack<String>();

int level;

String delim;

// ???? strange behavior by String.split() ????

delim = “\\n”;

String[] paths = input.split(Pattern.quote(delim));

// **** ****

for (String s : paths) {

// **** get the directory level ****

level = dirLevel(s);

// ???? remove \t from file name (strange behavior by String.replaceAll() ????

delim = “\\t”;

s = s.replaceAll(Pattern.quote(delim), “”);

// **** is this a file or folder ****

if (isDirectory(s)) {

// **** adjust the directory path stack ****

for ( ; currentLevel >= level; currentLevel–) {

pathStack.pop();

}

// **** push current directory and adjust current directory level ****

pathStack.push(s);

currentLevel++;

}

// **** this is a file ****

else {

// **** build the full path of the file ****

String fullPath = buildFullPath(pathStack, s);

// **** compute the length and update the max length (if needed) ****

int length = fullPath.length();

if (length > maxLength) {

maxLength = length;

}

}

}

// **** return the length of the max path ****

return maxLength;

}

Vector help wanted smiley

I actually implemented two solutions. Both are somewhat similar and both exhibit the same strange results and behavior. As you can see on the actual code, String.split() and String.replaceAll() require the use of Pattern.quote() to work with the delimiter string “\\n”. They do not seem to work with “\n” or “\\n” strings when used without the Pattern.quote() method.

I have restarted a couple times my Eclipse IDE. Given that I have many windows open at this time I have not restarted my computer yet. Will do that over the weekend and if there is a change will update this post.

If you have comments or questions regarding this or any other post in this blog please do not hesitate and send me a message. Will reply as soon as possible and will not use your name unless you explicitly allow me to do so.

John

john.canessa@gmail.com

Follow me on Twitter:  @john_canessa

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.