LeetCode in Kotlin

676. Implement Magic Dictionary

Medium

Design a data structure that is initialized with a list of different words. Provided a string, you should determine if you can change exactly one character in this string to match any word in the data structure.

Implement the MagicDictionary class:

Example 1:

Input

[“MagicDictionary”, “buildDict”, “search”, “search”, “search”, “search”] [[],

[[“hello”, “leetcode”]], [“hello”], [“hhllo”], [“hell”], [“leetcoded”]]

Output: [null, null, false, true, false, false]

Explanation:

MagicDictionary magicDictionary = new MagicDictionary(); 
magicDictionary.buildDict(["hello", "leetcode"]); 
magicDictionary.search("hello"); // return False 
magicDictionary.search("hhllo"); // We can change the second 'h' to 'e' to match "hello" so we return True 
magicDictionary.search("hell"); // return False 
magicDictionary.search("leetcoded"); // return False

Constraints:

Solution

class MagicDictionary {
    private var dictionaryWords: Array<String>? = null
    fun buildDict(dictionary: Array<String>?) {
        dictionaryWords = dictionary
    }

    fun search(searchWord: String): Boolean {
        for (word in dictionaryWords!!) {
            if (isOffByOneLetter(word, searchWord)) {
                return true
            }
        }
        return false
    }

    private fun isOffByOneLetter(word: String, searchWord: String): Boolean {
        if (isDifferentLengths(word, searchWord) || word == searchWord) {
            return false
        }
        var numDifferentLetters = 0
        for (i in word.indices) {
            if (isNotTheSameLetter(word[i], searchWord[i])) {
                numDifferentLetters++
            }
            if (numDifferentLetters > 1) {
                return false
            }
        }
        return numDifferentLetters == 1
    }

    private fun isDifferentLengths(word: String, searchWord: String): Boolean {
        return word.length != searchWord.length
    }

    private fun isNotTheSameLetter(c1: Char, c2: Char): Boolean {
        return c1 != c2
    }
}

/*
 * Your MagicDictionary object will be instantiated and called as such:
 * var obj = MagicDictionary()
 * obj.buildDict(dictionary)
 * var param_2 = obj.search(searchWord)
 */