算法设计与分析(一)

890.Question

You have a list of words and a pattern, and you want to know which words in words matches the pattern.

A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.

(Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.)

Return a list of the words in words that match the given pattern.
You may return the answer in any order.

Example

Inpu: words = [“abc”,”deq”,”mee”,”aqq”,”dkd”,”ccc”], pattern = “abb”
Output:[“mee”,”aqq”]
Explanation: “mee” matches the pattern because there is a permutation {a -> m, b -> e, …}.
“ccc” does not match the pattern because {a -> c, b -> c, …} is not a permutation,
since a and b map to the same letter.

Note

  • 1 <= words.length <= 50
  • 1 <= pattern.length = words[i].length <= 20

思路

这道题目说明白了就是比较特殊的字符串匹配,需要在词库里找出符合例如“abb”、“aabb”范例的单词,有点像小学语文考试要我们写出“绿油油”、“红红火火”这些词语的题目。

一开始我的想法是把单词里的每个不同的字母按顺序放进数组里,然后根据范例拼出一个新的单词,如果与原单词一致则符合范例。

例如,对于单词“mee”,能得到数组[m,e],根据范例“abb”拼出“mee”,根据范例“aabb”则拼出“mmee”。对于单词“xyz”,数组为[x,y,z],拼出单词“xyy”和“xxyy”。

这个算法的好处就是不用考虑原单词里的字母个数,因为字母个数与范例不同的话拼出来的新单词肯定与原单词不一致。

但这种方法却是错误的。这种算法有一个前提就是范例的字母出现顺序必须按照字典序,即不能出现“baa”、“baba”这样的范例,“b”不能比“a”更早出现。因为求新单词时,我是根据letter[pattern[i]-‘a’]来获取对应字母的,单词“mee”根据范例“baa”得到的新单词是“emm”(字母表为[m,e]),符合范例的单词就会被判定为不符合。

所以,我只能改变算法了。现在我利用一个[a..z]的数组,数组下标为单词的字母,存放的内容则是范例里对应位置的字母。还是以单词“mee”和范例“abb”为例,能得到letter[m]=a,letter[e]=b。我们遍历单词里的每个字母,然后根据以下规则来判断单词是否与范例一致:

  • letter[index]为空 ,如果pattern[index]已经被使用,则单词与范例不一致,否则letter[index] = pattern[index] 。

    单词“xyz”,范例“abb”
    ①letter[x] = a
    ②letter[y] = b
    ③letter[z]虽然为空,但b已经被使用,因此单词与范例不一致

  • letter[index]不为空,如果letter[index]不等于pattern[index],则单词与范例不一致。

    单词“xyy”,范例“abc”
    ①letter[x] = a
    ②letter[y] = b
    ③letter[y]不为空,并且letter[y] != c,可以判断出单词与范例不一致

代码

class Solution 
{
  public:
    vector<string> findAndReplacePattern(vector<string>& words, string pattern) 
    {
      vector<string> wordsMatched;
      char letter[26];
      bool checkUsed[26];
      bool valid;
      //遍历单词的每个字母
      for (int i = 0; i < words.size(); i++)
      {
        //长度不一致就不需要做下面的判断操作了
        if (words[i].length() != pattern.length()) continue;

        //初始化两个判断用的数组
        for (int j = 0; j < 30; j++)
        {
          letter[j] = '?';
          checkUsed[j] = false;
        }

        valid = true;
        for (int j = 0 ; j < words[i].length(); j++)
          if (letter[words[i][j]-'a'] == '?')  //letter[index]为空
          {
            if (checkUsed[pattern[j]-'a'])
            {
              valid = false;
              break;
            }
            letter[words[i][j]-'a'] = pattern[j];
            checkUsed[pattern[j]-'a'] = true;
          }
          else  //letter[index]不为空
          {
            if (letter[words[i][j]-'a'] != pattern[j])
            {
              valid = false;
              break;
            }
          }
        //单词与范例一致就放进返回数组里
        if (valid) wordsMatched.push_back(words[i]);
      }
      return wordsMatched;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值