class Anagram(val internalString: String) {
    val letterMap: Map<Char, Int> by lazy {
        val temp = emptyMap<Char, Int>().toMutableMap();
        for (i in internalString){
            if (temp.containsKey(i)){
                temp.put(i.toLowerCase(), temp[i]!!.inc())
            }
            else temp.put(i.toLowerCase(), 1)
        }
        temp
    }

    fun match(wordList: List<String>): List<String>{
        return wordList.filter { isAnagram(it) }
    }

    private fun isAnagram(candidate: String): Boolean{
        if (internalString.toLowerCase() == candidate.toLowerCase()) return false
        val candidateLetterMap = emptyMap<Char, Int>().toMutableMap()
        for (i in candidate){
            if (candidateLetterMap.containsKey(i)){
                candidateLetterMap.put(i.toLowerCase(), candidateLetterMap[i]!!.inc())
            }
            else candidateLetterMap.put(i.toLowerCase(), 1)
        }
        return candidateLetterMap == letterMap
    }
}