https://leetcode.com/problems/word-pattern Easy

Решение

class Solution {
    fun wordPattern(pattern: String, s: String): Boolean {
        val words = s.split(' ')
        if (words.size != pattern.length) return false
        val p2w = HashMap<Char, String>(pattern.length * 2)
        val w2p = HashMap<String, Char>(pattern.length * 2)
        var i = 0
        while (i < pattern.length) {
            val c = pattern[i]
            val w = words[i]
            val mw = p2w[c]
            if (mw == null) {
                if (w2p.containsKey(w)) return false
                p2w[c] = w
                w2p[w] = c
            } else if (mw != w) return false
            i++
        }
        return true
    }
}