290. 单词规律-LeetCode(C++)

290. 单词规律

2024.9.12

题目

给定一种规律 pattern 和一个字符串 s ,判断 s 是否遵循相同的规律。

这里的 遵循 指完全匹配,例如, pattern 里的每个字母和字符串 s 中的每个非空单词之间存在着双向连接的对应规律。

提示:

  • 1 <= pattern.length <= 300
  • pattern 只包含小写英文字母
  • 1 <= s.length <= 3000
  • s 只包含小写英文字母和 ' '
  • s 不包含 任何前导或尾随对空格
  • s 中每个单词都被 单个空格 分隔
示例

示例1:

输入: pattern = "abba", s = "dog cat cat dog"
输出: true

示例 2:

输入:pattern = "abba", s = "dog cat cat fish"
输出: false

示例 3:

输入: pattern = "aaaa", s = "dog cat cat dog"
输出: false
反思

1.我们得到了一个新的角度来看待hashmap,即:键与值之间的单射关系。如果我们想维护双射关系,那么必须用到两个hashmap

2.还是老问题啊,多考虑下边界,有可能pattern和s数量不对等。

3.多摸摸c++基础语法。

题解

【双向连接的对应规律】,这说明pattern和每一个子串之间是双射关系,即pattern[i] 可以确定一个 string,一个string也可以确定一个pattern[i]。

class Solution {
public:
    std::vector<std::string> splitStringBySpace(const std::string& str) {
        std::vector<std::string> result;
        std::istringstream iss(str);
        std::string word;

        while (iss >> word) {
            result.push_back(word);
        }

        return result;
    }

    bool wordPattern(string pattern, string s) {
        std::vector<std::string> words = splitStringBySpace(s);
        if (pattern.size() != words.size()) {
            return false;
        }
        std::unordered_map<char, string> char2string;
        std::unordered_map<string, char> string2char;
        for (int i = 0; i < pattern.size(); i++) {
            char c = pattern[i];
            std::string word = words[i];
            if (char2string.find(c) != char2string.end()) {
                if (char2string[c] != word) {
                    return false;
                }
            } else {
                char2string[c] = word;
            }
            if (string2char.find(word) != string2char.end()) {
                if (string2char[word] != c) {
                    return false;
                }
            } else {
                string2char[word] = c;
            }
        }
        return true;
    }
};
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是电话号码的字母组合问题的 C++ 代码实现: ``` class Solution { public: vector<string> letterCombinations(string digits) { unordered_map<char, string> mapping = { {'2', "abc"}, {'3', "def"}, {'4', "ghi"}, {'5', "jkl"}, {'6', "mno"}, {'7', "pqrs"}, {'8', "tuv"}, {'9', "wxyz"} }; vector<string> res; if (digits.empty()) { return res; } string combination; backtrack(res, combination, digits, 0, mapping); return res; } void backtrack(vector<string>& res, string& combination, string& digits, int index, unordered_map<char, string>& mapping) { if (index == digits.size()) { res.push_back(combination); return; } for (char c : mapping[digits[index]]) { combination.push_back(c); backtrack(res, combination, digits, index + 1, mapping); combination.pop_back(); } } }; ``` 其中 `letterCombinations` 函数用来生成所有的字母组合,`backtrack` 函数用来进行回溯操作。在 `letterCombinations` 函数中,首先根据数字字符和字母的映射关系创建了一个 `unordered_map` 对象 `mapping`。然后定义了一个空字符串 `combination` 和一个空 vector `res` 来保存最终结果。最后调用了 `backtrack` 函数来生成所有的字母组合。在 `backtrack` 函数中,首先判断是否达到了数字字符串的末尾,如果是,则将当前的 `combination` 字符串保存到 `res` 中。否则,遍历当前数字字符所能表示的所有字母,依次加入到 `combination` 字符串中,然后递归调用 `backtrack` 函数,添加下一个数字字符所能表示的字母。递归完成后,需要将 `combination` 字符串还原到上一个状态,以便进行下一次回溯。最终返回 `res` 数组即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值