forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestCommonPrefix.java
48 lines (43 loc) · 1.55 KB
/
LongestCommonPrefix.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
package com.leetcode.strings;
/**
* Level: Easy
* Problem: https://leetcode.com/problems/longest-common-prefix/
*
* @author rampatra
* @since 2019-04-20
*/
public class LongestCommonPrefix {
/**
* Time complexity: O(r * c)
* where,
* r = no. of strings
* c = max. no. of characters in a particular string
* <p>
* Runtime: <a href="https://leetcode.com/submissions/detail/223737988/">1 ms</a>.
*
* @param strs
* @return
*/
public static String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0) return "";
int row;
for (int col = 0; col < strs[0].length(); col++) {
for (row = 0; row < strs.length - 1; row++) {
// once we find a different character under one column, return the characters read so far
if (col == strs[row].length()
|| col == strs[row + 1].length()
|| strs[row].charAt(col) != strs[row + 1].charAt(col)) {
return strs[row].substring(0, col);
}
}
}
return strs[0];
}
public static void main(String[] args) {
System.out.println(longestCommonPrefix(new String[]{}));
System.out.println(longestCommonPrefix(new String[]{""}));
System.out.println(longestCommonPrefix(new String[]{"a"}));
System.out.println(longestCommonPrefix(new String[]{"flower", "flow", "flight"}));
System.out.println(longestCommonPrefix(new String[]{"dog", "racecar", "car"}));
}
}