Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
There is no common prefix among the input strings.
1 <= strs.length <= 2000 <= strs[i].length <= 200strs[i] consists of only lowercase English letters if it is non-empty.No developer has claimed the Ruby throne for LeetCode #14 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
# @param {String[]} strs
# @return {String}
def longest_common_prefix(strs)
result = ''
(0...strs[0].size).each do |i|
char = strs[0][i]
strs[1..].each do |str|
if str[i] != char
return result
end
end
result << char
end
result
end
No developer has claimed the Python throne for LeetCode #14 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
result = ''
for i in range(len(strs[0])):
char = strs[0][i]
for string in strs[1:]:
if i >= len(string) or string[i] != char:
return result
result += char
return result
No developer has claimed the Java throne for LeetCode #14 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
class Solution {
public String longestCommonPrefix(String[] strs) {
var result = "";
for (var i = 0; i < strs[0].length(); i++) {
var c = strs[0].charAt(i);
for (var j = 1; j < strs.length; j++) {
if (i >= strs[j].length() || strs[j].charAt(i) != c) {
return result;
}
}
result += c;
}
return result;
}
}
No developer has claimed the C++ throne for LeetCode #14 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Javascript throne for LeetCode #14 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Go throne for LeetCode #14 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Rust throne for LeetCode #14 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the C# throne for LeetCode #14 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #14 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #14 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.