-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonPrefix.java
More file actions
66 lines (56 loc) · 1.94 KB
/
Copy pathLongestCommonPrefix.java
File metadata and controls
66 lines (56 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.interviewbit.string_parsing;
import com.util.LogUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author neeraj on 2019-07-31
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class LongestCommonPrefix {
static class MinLength {
String input;
int index;
int length = Integer.MAX_VALUE;
}
public static void main(String[] args) {
solve(Arrays.asList("abcdefgh", "aefghijk", "abcefgh"));
solve(Arrays.asList("abab", "ab", "abcd"));
solve(Arrays.asList("ABCD"));
}
public static void solve(List<String> inputs) {
ArrayList<String> inputList = new ArrayList<>(inputs);
LogUtil.logIt("Longest Common Prefix in " +
inputs + " is ===> " + longestCommonPrefix(inputList));
}
public static String longestCommonPrefix(ArrayList<String> inputList) {
final MinLength minLength = new MinLength();
// First find out the String of minimum length in the inputList.
for (int i = 0; i < inputList.size(); i++) {
if (inputList.get(i).length() < minLength.length) {
minLength.length = inputList.get(i).length();
minLength.input = inputList.get(i);
minLength.index = i;
}
}
int index = 1;
String prefix = "";
String result = "";
while (true && index <= minLength.length) {
prefix = minLength.input.substring(0, index++);
if (isCommonPrefixForAllInput(inputList, prefix)) {
result = prefix;
continue;
} else {
break;
}
}
return result;
}
public static boolean isCommonPrefixForAllInput(ArrayList<String> inputList, String prefix) {
return inputList
.parallelStream()
.allMatch(input -> input.startsWith(prefix));
}
}