Longest Common Prefix
Input:
["flower","flow","flight"]
Output:
"fl"Input:
["dog","racecar","car"]
Output:
""
Explanation:
There is no common prefix among the input strings.Note
Code
Last updated
Input:
["flower","flow","flight"]
Output:
"fl"Input:
["dog","racecar","car"]
Output:
""
Explanation:
There is no common prefix among the input strings.Last updated
class Solution {
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0 ) return "";
String res = strs[0];
for (int i = 1; i < strs.length; i++) {
while (strs[i].indexOf(res) != 0) {
res = res.substring(0, res.length() - 1);
}
}
return res;
}
}