-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathLongestCommonPrefix.java
More file actions
64 lines (48 loc) · 1.95 KB
/
Copy pathLongestCommonPrefix.java
File metadata and controls
64 lines (48 loc) · 1.95 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
/**
* GFG link: https://practice.geeksforgeeks.org/problems/longest-common-prefix-in-an-array5129/1
* This question can be solved in many ways, but this is the Trie or Prefix-Tree implementation for it.
* assuming the strings contain only lowercase words(the TrieNode data member 'next' data structure can be modified based on the requirement)
*/
public class LongestCommonPrefix {
public static void main(String[] args) {
Solution solution = new Solution();
String[] words = {"geeks", "geeksforgeeks", "geekds", "geekfs"};
System.out.println(solution.lcs(words));
}
static class Solution {
public String lcs(String[] words) {
TrieNode root = new TrieNode();
for (String word: words)
createTrie(word, 0, root);
int len = search(words[0], 0, root, words.length);
return len > 0 ? words[0].substring(0, len) : "-1";
}
//Trie class
private class TrieNode {
TrieNode[] next;
int count; //how many strings is the current substring a prefix
TrieNode() {
next = new TrieNode[26];
count = 0;
}
}
private void createTrie(String s, int in, TrieNode parent) {
if(in == s.length()) {
return;
}
if(parent.next[s.charAt(in) - 'a'] == null)
parent.next[s.charAt(in) - 'a'] = new TrieNode();
parent.next[s.charAt(in) - 'a'].count++;
createTrie(s, in + 1, parent.next[s.charAt(in) - 'a']);
}
private int search(String s, int in, TrieNode parent, int totalWords) {
if(in == s.length()) {
return s.length();
}
if(parent.next[s.charAt(in) - 'a'].count != totalWords) {
return in;
}
return search(s, in + 1, parent.next[s.charAt(in) - 'a'], totalWords);
}
}
}