LeetCode 2744.最大字符串配对数目

给你一个下标从 0 开始的数组 words ,数组中包含 互不相同 的字符串。

如果字符串 words[i] 与字符串 words[j] 满足以下条件,我们称它们可以匹配:

字符串 words[i] 等于 words[j] 的反转字符串。
0 <= i < j < words.length
请你返回数组 words 中的 最大 匹配数目。

注意,每个字符串最多匹配一次。

示例 1:

输入:words = [“cd”,“ac”,“dc”,“ca”,“zz”]
输出:2
解释:在此示例中,我们可以通过以下方式匹配 2 对字符串:

  • 我们将第 0 个字符串与第 2 个字符串匹配,因为 word[0] 的反转字符串是 “dc” 并且等于 words[2]。
  • 我们将第 1 个字符串与第 3 个字符串匹配,因为 word[1] 的反转字符串是 “ca” 并且等于 words[3]。
    可以证明最多匹配数目是 2 。
    示例 2:

输入:words = [“ab”,“ba”,“cc”]
输出:1
解释:在此示例中,我们可以通过以下方式匹配 1 对字符串:

  • 我们将第 0 个字符串与第 1 个字符串匹配,因为 words[1] 的反转字符串 “ab” 与 words[0] 相等。
    可以证明最多匹配数目是 1 。
    示例 3:

输入:words = [“aa”,“ab”]
输出:0
解释:这个例子中,无法匹配任何字符串。

提示:

1 <= words.length <= 50
words[i].length == 2
words 包含的字符串互不相同。
words[i] 只包含小写英文字母。

法一:直接模拟:

class Solution {
public:
    int maximumNumberOfStringPairs(vector<string>& words) {
        int ans = 0;
        for (int i = 0; i < words.size(); ++i)
        {
            for (int j = i + 1; j < words.size(); ++j)
            {
                if (words[i][0] == words[j][1] && words[i][1] == words[j][0])
                {
                    ++ans;
                    break;
                }
            }
        }

        return ans;
    }
};

如果words长度长度为n,此算法时间复杂度为O(n 2 ^{2} 2),空间复杂度为O(1)。

法二:将法一中的比较过程改用标准库,更普适:

class Solution {
public:
    int maximumNumberOfStringPairs(vector<string>& words) {
        int ans = 0;
        for (int i = 0; i < words.size(); ++i)
        {
            reverse(words[i].begin(), words[i].end());
            for (int j = i + 1; j < words.size(); ++j)
            {
                if (words[i] == words[j])
                {
                    ++ans;
                    break;
                }
            }
        }

        return ans;
    }
};

如果words长度长度为n,此算法时间复杂度为O(n 2 ^{2} 2),空间复杂度为O(1)。

法三:哈希表,由于words中各个元素都不相同,每当遍历到一个元素,在unordered_set中查找目标哈希值,然后将当前遍历到的元素的哈希值存入unordered_set中:

class Solution {
public:
    int maximumNumberOfStringPairs(vector<string>& words) {
        unordered_set<int> ui;
        int ans = 0;
        for (string &s : words)
        {
            if (ui.find((s[1] << 8) + s[0]) != ui.end())
            {
                ++ans;
            }
            ui.insert((s[0] << 8) + s[1]);
        }

        return ans;
    }
};

如果words长度长度为n,此算法时间复杂度为O(n),空间复杂度为O(n)。

法四:字典树,又称Trie、前缀树、单词查找树、前缀树,它利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较:

class Solution {
public:
    int maximumNumberOfStringPairs(vector<string>& words) {
        root = new TrieNode;
        int ans = 0;
        for (string &word : words)
        {
            string reversedString = word;
            reverse(reversedString.begin(), reversedString.end());
            ans += search(root, reversedString);
            insert(root, word);
        }

        return ans;
    }

private:
    class TrieNode
    {
    public:
        unordered_map<char, TrieNode *> children;
        int count = 0;
    };

    TrieNode *root;

    int search(TrieNode *root, string &s)
    {
        for (char c : s)
        {
            if (root->children.find(c) == root->children.end())
            {
                return 0;
            }
            root = root->children[c];
        }

        return root->count;
    }

    void insert(TrieNode *root, string &s)
    {
        for (char c : s)
        {
            if (root->children.find(c) == root->children.end())
            {
                root->children[c] = new TrieNode;
            }
            root = root->children[c];
        }

        ++root->count;
    }
};

如果words的长度为n,每个word的平均长度为m(本题中m为常数2),则此算法时间复杂度为O(nm),空间复杂度最差为O(nm)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值