LeetCode 솔루션					분류
				
						[Easy - wk1 - Q3] 14. Longest Common Prefix
본문
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 lower-case English letters.
관련자료
- 
			링크
			댓글 6
					
			yuun님의 댓글
- 익명
- 작성일
class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        prefix = ''
        if not strs:
            return prefix
        
        mn = 10**10
        for s in strs:
            if len(s) < mn:
                mn = len(s)
        
        i = 0
        while i < mn:
            start = strs[0][i]
            for j in range(1, len(strs)):
                if start == strs[j][i]:
                    continue
                else:
                    return prefix
            prefix+=start
            i+=1
        return prefixJack님의 댓글
- 익명
- 작성일
class Solution {
    public String longestCommonPrefix(String[] strs) {
        
        if(strs == null || strs.length == 0){
            return "";
        }
        
        for(int i=0; i<strs[0].length(); i++){
            char ch = strs[0].charAt(i);
            for(int j=1; j<strs.length; j++){
                if(i == strs[j].length() || strs[j].charAt(i) != ch){
                    return strs[0].substring(0,i);
                }
            }
        }
        
        return strs[0];
    }
}Jack님의 댓글
- 익명
- 작성일
					
										
					Python
Runtime: 38 ms, faster than 77.68% of Python3 online submissions for Longest Common Prefix.
Memory Usage: 14 MB, less than 51.33% of Python3 online submissions for Longest Common Prefix.
				
													
								Runtime: 38 ms, faster than 77.68% of Python3 online submissions for Longest Common Prefix.
Memory Usage: 14 MB, less than 51.33% of Python3 online submissions for Longest Common Prefix.
class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        prefix=""
        j=0
        result=False
        
        if len(strs) == 1:
            return strs[0]
        
        strs.sort(key=len)
        
        for s in strs[0]:
            for i in range(1, len(strs)):
                if s == strs[i][j]:
                    result=True
                else:
                    result=False
                    break
            j+=1
            if result:
                prefix+=s
            else:
                break
        return prefixChloe님의 댓글
- 익명
- 작성일
class Solution(object):
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        if not strs:
            return ""
        shortest = min(strs, key=len)
        for i, ch in enumerate(shortest):
            for other in strs:
                if other[i] != ch:
                    shortest = strs[0][:i]
                    return shortest
        
        return shortestdawn27님의 댓글
- 익명
- 작성일
var longestCommonPrefix = function (strs) {
  let prefixComparer = strs[0];
  let prefix = [];
  if (strs.length === 1) return strs[0];
  if (strs.length === 0) return '';
  if (strs[0][0] !== strs[1][0]) return '';
  if (strs.length <= 2) return strs[0];
  for (let i = 0; i < strs.length; i++) {
    for (let j = 0; j < strs[i].length; j++) {
      if (prefixComparer[j] === strs[i][j]) {
        prefix.push(strs[i][j]);
      } else if (prefixComparer[j] !== strs[i][j]) {
        prefix = prefix.slice(0, j);
      }
    }
  }
  prefix = prefix.join('');
  return prefix.length > 1 ? prefix : ''; 
								 
							







