LeetCode – Longest Common Prefix (Java)
Problem
Write a function to find the longest common prefix string amongst an array of strings.
Analysis
To solve this problem, we need to find the two loop conditions. One is the length of the shortest string. The other is iteration over every element of the string array.
Java Solution
public String longestCommonPrefix(String[] strs) { if(strs==null || strs.length ==0){ return ""; } if(strs.length == 1){ return strs[0]; } int i=0; while(true){ boolean flag = true; for(int j=1; j<strs.length; j++){ if(strs[j].length()<=i || strs[j-1].length() <=i || strs[j].charAt(i) != strs[j-1].charAt(i)){ flag = false; break; } } if(flag){ i++; }else{ break; } } return strs[0].substring(0, i); } |
<pre><code> String foo = "bar"; </code></pre>
-
Satish
-
Rahul Jain
-
Narendra
-
Bukary Kandeh
-
Bukary Kandeh
-
Vasubabu
-
FooBarBaz
-
SA
-
TheForceAwakens
-
Rudolf Eremyan
-
Mike
-
Juan Melo
-
b
-
Rishabh Sanghvi
-
CRH
-
Shannon McNeill
-
BobTheSCV