All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0201_0300.s0290_word_pattern.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0201_0300.s0290_word_pattern

// #Easy #String #Hash_Table #Data_Structure_II_Day_7_String
// #2022_11_04_Time_177_ms_(89.55%)_Space_35.2_MB_(95.52%)

class Solution {
    fun wordPattern(pattern: String, s: String): Boolean {
        val map: MutableMap = HashMap()
        val words = s.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        if (words.size != pattern.length) {
            return false
        }
        for (i in 0 until pattern.length) {
            if (!map.containsKey(pattern[i])) {
                if (map.containsValue(words[i])) {
                    return false
                }
                map[pattern[i]] = words[i]
            } else {
                if (words[i] != map[pattern[i]]) {
                    return false
                }
            }
        }
        return true
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy