Skip to content

Latest commit

 

History

History
52 lines (39 loc) · 1.27 KB

_3043. Find the Length of the Longest Common Prefix.md

File metadata and controls

52 lines (39 loc) · 1.27 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 28, 2024

Last updated : June 28, 2024


Related Topics : Array, Hash Table, String, Trie

Acceptance Rate : 56.19 %


Solutions

Python

class Solution:
    def longestCommonPrefix(self, arr1: List[int], arr2: List[int]) -> int:
        trie = {}

        for s in arr1 :
            curr = trie
            for c in str(s) :
                if c not in curr :
                    curr[c] = {}
                curr = curr[c]
            curr[False] = True
        
        maxx = 0
        for s in arr2 :
            curr = trie
            counter = 0
            for c in str(s) :
                if c not in curr :
                    break
                counter += 1
                curr = curr[c]
            maxx = max(maxx, counter)

        return maxx