Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0388c5e

Browse files
refactor 388
1 parent 2b0b658 commit 0388c5e

File tree

2 files changed

+153
-130
lines changed

2 files changed

+153
-130
lines changed
Lines changed: 89 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,109 @@
11
package com.fishercoder.solutions;
22

3-
import com.fishercoder.common.utils.CommonUtils;
4-
53
import java.util.Stack;
64

5+
/**
6+
* 388. Longest Absolute File Path
7+
*
8+
* Suppose we abstract our file system by a string in the following manner:
9+
*
10+
* The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents:
11+
*
12+
* dir
13+
* subdir1
14+
* subdir2
15+
* file.ext
16+
* The directory dir contains an empty sub-directory subdir1 and a sub-directory subdir2 containing a file file.ext.
17+
*
18+
* The string "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext" represents:
19+
*
20+
* dir
21+
* subdir1
22+
* file1.ext
23+
* subsubdir1
24+
* subdir2
25+
* subsubdir2
26+
* file2.ext
27+
* The directory dir contains two sub-directories subdir1 and subdir2. subdir1 contains a file file1.ext and an empty second-level sub-directory subsubdir1.
28+
* subdir2 contains a second-level sub-directory subsubdir2 containing a file file2.ext.
29+
*
30+
* We are interested in finding the longest (number of characters) absolute path to a file within our file system.
31+
* For example, in the second example above, the longest absolute path is "dir/subdir2/subsubdir2/file2.ext", and its length is 32 (not including the double quotes).
32+
*
33+
* Given a string representing the file system in the above format, return the length of the longest absolute path to file in the abstracted file system. If there is no file in the system, return 0.
34+
*
35+
* Note:
36+
*
37+
* The name of a file contains at least a . and an extension.
38+
* The name of a directory or sub-directory will not contain a ..
39+
* Time complexity required: O(n) where n is the size of the input string.
40+
*
41+
* Notice that a/aa/aaa/file1.txt is not the longest file path, if there is another path aaaaaaaaaaaaaaaaaaaaa/sth.png.*/
742
public class _388 {
8-
public static int lengthLongestPath(String input) {
9-
Stack<Integer> stack = new Stack();
10-
int longestLen = 0;
11-
int currDirLen = 0;
12-
int i = 0;
13-
int currLevel = 0;
14-
int nextLevel = 0;
15-
boolean isFile = false;
16-
Character period = '.';
17-
Character space = ' ';
18-
while (i < input.length()) {
19-
currLevel = nextLevel;
20-
int currStrLen = 0;
21-
while (i < input.length() && (Character.isLetterOrDigit(input.charAt(i))
43+
public static class Solution1 {
44+
public int lengthLongestPath(String input) {
45+
Stack<Integer> stack = new Stack();
46+
int longestLen = 0;
47+
int currDirLen = 0;
48+
int i = 0;
49+
int currLevel = 0;
50+
int nextLevel = 0;
51+
boolean isFile = false;
52+
Character period = '.';
53+
Character space = ' ';
54+
while (i < input.length()) {
55+
currLevel = nextLevel;
56+
int currStrLen = 0;
57+
while (i < input.length() && (Character.isLetterOrDigit(input.charAt(i))
2258
|| period.equals(input.charAt(i)) || space.equals(input.charAt(i)))) {
23-
if (period.equals(input.charAt(i))) {
24-
isFile = true;
59+
if (period.equals(input.charAt(i))) {
60+
isFile = true;
61+
}
62+
i++;
63+
currStrLen++;
64+
}
65+
if (isFile) {
66+
longestLen = Math.max(longestLen, currDirLen + currStrLen);
67+
} else {
68+
currDirLen += currStrLen + 1;
69+
stack.push(currStrLen + 1);
2570
}
26-
i++;
27-
currStrLen++;
28-
}
29-
if (isFile) {
30-
longestLen = Math.max(longestLen, currDirLen + currStrLen);
31-
} else {
32-
currDirLen += currStrLen + 1;
33-
stack.push(currStrLen + 1);
34-
}
3571

36-
nextLevel = 0;
37-
i = i + 1;//increment one to let it pass "\n" and start from "\t"
38-
while (i < input.length() - 1 && input.substring(i, i + 1).equals("\t")) {
39-
nextLevel++;
40-
i = i + 1;
41-
}
72+
nextLevel = 0;
73+
i = i + 1;//increment one to let it pass "\n" and start from "\t"
74+
while (i < input.length() - 1 && input.substring(i, i + 1).equals("\t")) {
75+
nextLevel++;
76+
i = i + 1;
77+
}
4278

43-
if (nextLevel < currLevel) {
44-
int j = 0;
45-
if (isFile) {
46-
while (!stack.isEmpty() && j < (currLevel - nextLevel)) {
47-
currDirLen -= stack.pop();
48-
j++;
79+
if (nextLevel < currLevel) {
80+
int j = 0;
81+
if (isFile) {
82+
while (!stack.isEmpty() && j < (currLevel - nextLevel)) {
83+
currDirLen -= stack.pop();
84+
j++;
85+
}
86+
} else {
87+
while (!stack.isEmpty() && j <= (currLevel - nextLevel)) {
88+
currDirLen -= stack.pop();
89+
j++;
90+
}
4991
}
50-
} else {
51-
while (!stack.isEmpty() && j <= (currLevel - nextLevel)) {
92+
} else if (nextLevel == currLevel) {
93+
if (!isFile && !stack.isEmpty()) {
5294
currDirLen -= stack.pop();
53-
j++;
5495
}
5596
}
56-
} else if (nextLevel == currLevel) {
57-
if (!isFile && !stack.isEmpty()) {
58-
currDirLen -= stack.pop();
59-
}
60-
61-
}
6297

63-
if (nextLevel == 0) {
64-
currDirLen = 0;
65-
stack.clear();
66-
}
67-
68-
isFile = false;
69-
}
70-
71-
return longestLen;
72-
}
73-
74-
75-
public static void main(String... strings) {
76-
// System.out.println(Character.isLetterOrDigit('&'));
77-
// System.out.println(Character.isLetterOrDigit('\\'));
78-
// System.out.println(Character.isValidCodePoint('#'));
79-
String test = "\\t";
80-
// System.out.println(test.substring(0, 2).equals("\\t"));
81-
// System.out.print("\n\t");
82-
// System.out.print("something");
83-
// String s = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext";//correct output should be 32
84-
// String s = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext";//correct output is 20
85-
// String s = "aaaaaaaaaaaaaaaaaaaaa/sth.png";
86-
// String s = "a/aa/aaa/file1.txt";
87-
// String s = "file name with space.txt";
88-
// String s = "dir\n file.txt";
89-
String s = "dir\n file.txt";//correct output is 12
90-
// String s = "a\n\tb1\n\t\tf1.txt\n\taaaaa\n\t\tf2.txt";//correct answer is 14
91-
printWithIndex(s);
92-
System.out.println(s);
93-
System.out.println(lengthLongestPath(s));
94-
// System.out.println(parse(s));
95-
}
98+
if (nextLevel == 0) {
99+
currDirLen = 0;
100+
stack.clear();
101+
}
96102

97-
private static void printWithIndex(String s) {
98-
System.out.println("\\n");
99-
int len = s.length();
100-
for (int i = 0; i < len; i++) {
101-
System.out.print(i);
102-
System.out.print("\t");
103-
}
104-
System.out.println();
105-
Character slash = '\\';
106-
char space = ' ';
107-
char n = 'n';
108-
char t = 't';
109-
String newLine = "\\n";
110-
String newTab = "\\t";
111-
for (int i = 0; i < len; i++) {
112-
switch (s.charAt(i)) {
113-
case '\n':
114-
System.out.print("\\" + " " + "n");
115-
break;
116-
case '\t':
117-
System.out.print("\\" + " " + "t");
118-
break;
119-
default:
120-
System.out.print(s.charAt(i));
103+
isFile = false;
121104
}
122-
System.out.print("\t");
123-
}
124-
System.out.println();
125-
}
126-
127105

128-
public static int parse(String input) {
129-
String[] splits = input.split("\\n");
130-
CommonUtils.printArray_generic_type(splits);
131-
int longestLen = 0;
132-
for (String path : splits) {
133-
boolean isFile = false;
134-
int thisLen = 0;
135-
String[] paths = path.split("\\t");
136-
CommonUtils.printArray_generic_type(paths);
137-
if (paths[paths.length - 1].contains(".")) {
138-
isFile = true;
139-
}
140-
if (isFile) {
141-
for (String eachDir : paths) {
142-
thisLen += eachDir.length();
143-
thisLen++;//plus the slash sign
144-
}
145-
longestLen = Math.max(longestLen, thisLen);
146-
}
106+
return longestLen;
147107
}
148-
return longestLen;
149108
}
150109
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._388;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _388Test {
10+
private static _388.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _388.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(10, solution1.lengthLongestPath(
20+
"dir\\n\\tsubdir1\\n\\t\\tfile1.ext\\n\\t\\tsubsubdir1\\n\\tsubdir2\\n\\t\\tsubsubdir2\\n\\t\\t\\tfile2.ext"));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertEquals(9, solution1.lengthLongestPath(
26+
"dir\\n\\tsubdir1\\n\\tsubdir2\\n\\t\\tfile.ext"));
27+
}
28+
29+
@Test
30+
public void test3() {
31+
assertEquals(7, solution1.lengthLongestPath(
32+
"aaaaaaaaaaaaaaaaaaaaa/sth.png"));
33+
}
34+
35+
@Test
36+
public void test4() {
37+
assertEquals(9, solution1.lengthLongestPath(
38+
"a/aa/aaa/file1.txt"));
39+
}
40+
41+
@Test
42+
public void test5() {
43+
assertEquals(25, solution1.lengthLongestPath(
44+
"file name with space.txt"));
45+
}
46+
47+
@Test
48+
public void test6() {
49+
assertEquals(13, solution1.lengthLongestPath(
50+
"dir\\n file.txt"));
51+
}
52+
53+
@Test
54+
public void test7() {
55+
assertEquals(12, solution1.lengthLongestPath(
56+
"dir\n file.txt"));
57+
}
58+
59+
@Test
60+
public void test8() {
61+
assertEquals(7, solution1.lengthLongestPath(
62+
"a\\n\\tb1\\n\\t\\tf1.txt\\n\\taaaaa\\n\\t\\tf2.txt"));
63+
}
64+
}

0 commit comments

Comments
 (0)