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)。

相关推荐

  1. Leetcode2744. 字符串配对数目

    2024-02-20 23:36:04       58 阅读
  2. leetcode2744. 字符串配对数目

    2024-02-20 23:36:04       56 阅读
  3. LeetCode2744字符串配对数目

    2024-02-20 23:36:04       62 阅读
  4. LeetCode 2744.字符串配对数目

    2024-02-20 23:36:04       47 阅读
  5. Leetcode15-字符串配对数目2744

    2024-02-20 23:36:04       58 阅读
  6. python/c++ Leetcode题解——2744. 字符串配对数目

    2024-02-20 23:36:04       48 阅读
  7. LeetCode 2744.字符串配对数目:哈希表

    2024-02-20 23:36:04       58 阅读
  8. 【力扣每日一题】力扣2744字符串配对数目

    2024-02-20 23:36:04       62 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-20 23:36:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-20 23:36:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-20 23:36:04       87 阅读
  4. Python语言-面向对象

    2024-02-20 23:36:04       96 阅读

热门阅读

  1. 什么是机器学习

    2024-02-20 23:36:04       57 阅读
  2. PS的常用快捷方式有哪些?

    2024-02-20 23:36:04       54 阅读
  3. GET变量与POST变量

    2024-02-20 23:36:04       57 阅读
  4. 软考笔记--信息系统开发方法(上)

    2024-02-20 23:36:04       48 阅读
  5. CES 的Agent插件状态显示“故障”该如何处理?

    2024-02-20 23:36:04       55 阅读
  6. 游戏分组/王者荣耀

    2024-02-20 23:36:04       44 阅读