LeetCode - 890. Find and Replace Pattern

码农老姜 · · 351 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

链接:https://leetcode-cn.com/problems/find-and-replace-pattern/
难度:medium

挺水的一道题啊,为啥是medium

func findAndReplacePattern(words []string, pattern string) []string {
    var result []string
    for _, word := range words {
        if match(word, pattern) {
            result = append(result, word)
        }
    }
    return result
}

func match(word string, pattern string) bool {
    if len(word) != len(pattern) {
        return false
    } 

    m := map[byte]byte{}
    mp := map[byte]byte{}
    for i := 0; i < len(word); i++ {
        c := word[i]
        cp := pattern[i]
        if v, ok := m[c]; ok {
            if v != cp {
                return false
            }
        } else {
            if v, ok := mp[cp]; ok {
                if v != c {
                    return false
                }
            } else {
                mp[cp] = c
            }
            m[c] = cp
        }
    }
    return true
}

执行用时 : 0 ms, 在所有 Go 提交中击败了100.00%的用户
内存消耗 : 2.2 MB, 在所有 Go 提交中击败了100.00%的用户

估计是没人用golang提交过吧。。


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:码农老姜

查看原文:LeetCode - 890. Find and Replace Pattern

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

351 次点击  
加入收藏 微博
上一篇:设计模式
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传