Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Tiền tố chung dài nhất trong Python


Cho mảng chuỗi. Tìm tiền tố chung dài nhất. Nếu không có → "". Ví dụ: ["school","schedule","scotland"] → "sc".

Giải pháp

Lấy chuỗi đầu làm current. Duyệt các chuỗi còn lại, so sánh ký tự từng cặp, cắt current khi khác.

Ví dụ

Triển khai Python:

class Solution(object): 
    def longestCommonPrefix(self, strs): 
        if len(strs) == 0: 
            return "" 
        current = strs[0] 
        for i in range(1,len(strs)): 
            temp = "" 
            if len(current) == 0: 
                break 
            for j in range(len(strs[i])): 
                if j<len(current) and current[j] == strs[i][j]: 
                    temp+=current[j] 
                else: 
                    break 
            current = temp 
        return current 
input_list = ["school","schedule","scotland"] 
ob1 = Solution() 
print(ob1.longestCommonPrefix(input_list))

Đầu vào

["school","schedule","scotland"]

Đầu ra

"sc"