程式語言 - LeetCode - C++ - 3043. Find the Length of the Longest Common Prefix



題目:


解答:

class Solution {
public:
    int longestCommonPrefix(vector<int>& arr1, vector<int>& arr2) {
        unordered_set<int> mp;

        for (int n : arr1) {
            while (n > 0) {
                mp.insert(n);
                n /= 10;
            }
        }

        int ans = 0;

        for (int n : arr2) {
            while (n > 0) {
                if (mp.count(n)) {
                    ans = max(ans, static_cast<int>(to_string(n).size()));
                    break;
                }
                n /= 10;
            }
        }

        return ans;
    }
};