-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathLongestCommonPrefix.java
More file actions
53 lines (33 loc) · 959 Bytes
/
LongestCommonPrefix.java
File metadata and controls
53 lines (33 loc) · 959 Bytes
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
Problem Description
Given the array of strings A, you need to find the longest string S which is the prefix of ALL the strings in the array.
Longest common prefix for a pair of strings S1 and S2 is the longest string S which is the prefix of both S1 and S2.
For Example: longest common prefix of "abcdefgh" and "abcefgh" is "abc".
Example Input
Input 1:
A = ["abcdefgh", "aefghijk", "abcefgh"]
Input 2:
A = ["abab", "ab", "abcd"];
Example Output
Output 1:
"a"
Output 2:
"ab"
public class Solution {
public String longestCommonPrefix(String[] A) {
int min = Integer.MAX_VALUE;
String s = "";
for (int i = 0; i < A.length; i++) {
min = Integer.min(min, A[i].length());
}
String ans = "";
for (int i = 0; i < min; i++) {
char ch = A[0].charAt(i);
for (int j = 1; j < A.length; j++) {
if (A[j].charAt(i) != ch)
return ans;
}
ans += ch;
}
return ans;
}
}