Arena / Problems /

14. Longest Common Prefix

Easy 0/10 Thrones Claimed
Prev Next

Problem Specification

LeetCode #14

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 "".

Example 1
Input: strs = ["flower","flow","flight"]
Output: "fl"
Example 2
Input: strs = ["dog","racecar","car"]
Output: ""
Explanation:

There is no common prefix among the input strings.

Constraints
  • 1 <= strs.length <= 200
  • 0 <= strs[i].length <= 200
  • strs[i] consists of only lowercase English letters if it is non-empty.
Topics & Algorithms
⚔️

Vacant Ruby Throne

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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Ruby Solution by Anonymous Developer
0
Approach Intuition:
  1. To find the longest common prefix, you can scan from left to right, using the characters of the first string as the baseline.
  2. If the current character in the other strings does not match the baseline character, return the result. After each round of iteration, add the baseline character to the common prefix.
# @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

⚔️

Vacant Python Throne

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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Python Solution by Anonymous Developer
0
Approach Intuition:
  1. To find the longest common prefix, you can scan from left to right, using the characters of the first string as the baseline.
  2. If the current character in the other strings does not match the baseline character, return the result. After each round of iteration, add the baseline character to the common prefix.
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
⚔️

Vacant Java Throne

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.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Java Solution by Anonymous Developer
0
Approach Intuition:
  1. To find the longest common prefix, you can scan from left to right, using the characters of the first string as the baseline.
  2. If the current character in the other strings does not match the baseline character, return the result. After each round of iteration, add the baseline character to the common prefix.
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;
    }
}
⚔️

Vacant C++ Throne

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.

⚔️

Vacant Javascript Throne

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.

⚔️

Vacant Go Throne

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.

⚔️

Vacant Rust Throne

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.

⚔️

Vacant C# Throne

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.

⚔️

Vacant Swift Throne

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.

⚔️

Vacant Kotlin Throne

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.