码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • leetcode 890. Find and Replace Pattern(查找和替换pattern)


    Given a list of strings words and a string pattern, return a list of words[i] that match pattern. You may return the answer in any order.

    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.

    Example 1:

    Input: 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.

    Example 2:

    Input: words = [“a”,“b”,“c”], pattern = “a”
    Output: [“a”,“b”,“c”]

    返回words中和pattern匹配的单词list。
    和pattern匹配是指对应的字母匹配。多个字母不能同时匹配一个字母。

    思路:
    可以用hashmap保存映射关系,但是看到了一个更简洁的方法。

    从左到右遍历单词的字母时,如果匹配到了pattern的一个字母,
    比如mee和pattern “abb”
    m和a匹配,第一个e和第一个b匹配,它们的index是相同的;
    那么到了后面的e时,找到它出现的第一个index, 一定和b出现的第一个index一样,
    这个index相同就相当于在hashmap中找到了对应关系。

    用index模拟hashmap, 这样就不需要另存一个hashmap.

    public List<String> findAndReplacePattern(String[] words, String pattern) {
        List<String> res = new ArrayList<>();
        
        for(String word : words) {
            if(check(word, pattern)) res.add(word);
        }
        return res;
    }
    
    boolean check(String word, String pattern) {
        for(int i = 0; i < word.length(); i++) {
            if(word.indexOf(word.charAt(i)) != pattern.indexOf(pattern.charAt(i))) return false;
        }
        return true;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
  • 相关阅读:
    K线形态识别_镊子线
    文件(图片,视频)技术
    自己动手搭建一个传奇是什么体验?下面是我搭建的详细教程,大家跟着教程做,不光是学会了技术,平时还可以帮朋友搭建
    AI修复1950正年轻的他们;2022阿里天池冠军方案[1/1149];计算机优秀课程大集锦;贝叶斯统计课程资料;前沿论文 | ShowMeAI资讯日报
    云游戏发行是什么?云游戏发行的演进历程
    Python画图系列——折线图
    Win10一键重装系统后计算机图标怎么调出来
    UE5数字孪生系列笔记(一)
    LeetCode-剑指26-树的子结构
    《最新出炉》系列入门篇-Python+Playwright自动化测试-46-鼠标滚轮操作
  • 原文地址:https://blog.csdn.net/level_code/article/details/126058271
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号