Golang | Leetcode Golang题解之第290题单词规律

题目:

题解:

Go 复制代码
func wordPattern(pattern string, s string) bool {
    word2ch := map[string]byte{}
    ch2word := map[byte]string{}
    words := strings.Split(s, " ")
    if len(pattern) != len(words) {
        return false
    }
    for i, word := range words {
        ch := pattern[i]
        if word2ch[word] > 0 && word2ch[word] != ch || ch2word[ch] != "" && ch2word[ch] != word {
            return false
        }
        word2ch[word] = ch
        ch2word[ch] = word
    }
    return true
}
相关推荐
洛水水7 小时前
【力扣100题】18.随机链表的复制
算法·leetcode·链表
he___H14 小时前
接雨水----解
leetcode
洛水水16 小时前
【力扣100题】23. 螺旋矩阵
算法·leetcode·矩阵
~|Bernard|18 小时前
四,go语言中GMP调度模型
java·前端·golang
Tisfy18 小时前
LeetCode 2553.分割数组中数字的数位:模拟(maybe+翻转)——java也O(1)
java·数学·算法·leetcode·题解·模拟·取模
Controller-Inversion19 小时前
42. 接雨水
数据结构·算法·leetcode
Controller-Inversion19 小时前
33. 搜索旋转排序数组
数据结构·算法·leetcode
驼同学.19 小时前
【求职季】LeetCode Hot 100 渐进式扫盲手册(Python版)
python·算法·leetcode
宵时待雨19 小时前
优选算法专题6:模拟
数据结构·c++·算法·leetcode·职场和发展
Liangwei Lin19 小时前
LeetCode 35. 搜索插入位置
数据结构·算法·leetcode