-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path014_LongestCommonPrefix14.java
142 lines (128 loc) · 3.78 KB
/
014_LongestCommonPrefix14.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class Solution {
public static String longestCommonPrefix(String[] strs) {
if (strs.length == 0) return "";
Trie trie = new Trie();
String searchString = null;
for (String word : strs) {
if (searchString == null) {
searchString = word;
} else if (searchString.length() > word.length()) {
searchString = word;
}
trie.insert(word);
}
return trie.longestCommonPrefix(searchString);
}
}
class Trie {
public static class TrieNode {
Map<Character, TrieNode> map;
boolean isWord;
public TrieNode() {
map = new HashMap<>();
isWord = false;
}
}
private final TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
TrieNode current = root;
for (int i = 0; i < word.length(); i++) {
TrieNode newNode = current.map.get(word.charAt(i));
if (newNode == null) {
newNode = new TrieNode();
current.map.put(word.charAt(i), newNode);
}
current = newNode;
}
current.isWord = true;
}
public String longestCommonPrefix(String word) {
TrieNode current = root;
if (current.map.size() > 1) return "";
String longestCommonPrefix = "";
for (int i = 0; i < word.length(); i++) {
TrieNode newNode = current.map.get(word.charAt(i));
if (newNode == null || newNode.map.size() > 1) return longestCommonPrefix + word.charAt(i);
else {
longestCommonPrefix += word.charAt(i);
current = newNode;
}
}
return longestCommonPrefix;
}
}
*********************************************************************
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length == 0){
return "";
}
int count = lengthOfLongestCommonPrefix(strs);
return strs[0].substring(0, count);
}
private int lengthOfLongestCommonPrefix(String[] strs){
int count = 0;
for(int i = 0 ; i < strs[0].length(); i++){
boolean same = true;
char ch = ' ';
for(int j = 0; j < strs.length; j++){
if(i >= strs[j].length()){
return count;
}
if(j == 0){
ch = strs[j].charAt(i);
continue;
}
if(ch != strs[j].charAt(i)){
same = false;
}
}
if(same){
count ++;
}
else{
break;
}
}
return count;
}
}
*************************************************************************
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs.length==0)
return "";
boolean flag = true;
int count = 0;
int smallestLen = strs[0].length();
for(int i=1;i<strs.length;i++)
{
if(strs[i].length()<smallestLen)
smallestLen = strs[i].length();
}
for(int i=0;i<smallestLen;i++)
{
char c = strs[0].charAt(i);
for(int j=1;j<strs.length;j++)
{
if(strs[j].charAt(i)==c)
flag=true;
else
{
flag=false;
break;
}
}
if(flag==true)
count++;
else
break;
}
if(count==0)
return "";
return strs[0].substring(0,count);
}
}