Two HashMap(Pair,MutableList) + Rabin-Karp method
Approach\nTwo HashMap(Pair,MutableList) + Rabin-Karp method\nIn principle, a \u201Clight\u201D version of the solution is possible - instead of Rabin-Karp the u
# Approach\nTwo HashMap(Pair,MutableList) + Rabin-Karp method\nIn principle, a \u201Clight\u201D version of the solution is possible - instead of Rabin-Karp the usual comparison of strings, then you don\'t need the list and length, but you need to track the beginning of the string. It is much simpler in one way and a bit more complicated in another, and, probably, it will be inferior in speed.\n# Complexity\n![image.png](https://assets.leetcode.com/users/images/aa7c7b76-c233-4a99-ac24-9dce484e03d3_1726551241.769763.png)\n# Code\n```ver1 []\nclass Solution {\n    fun uncommonFromSentences(s1: String, s2: String): Array<String> {\n        val len1 = s1.length\n        val len2 = s2.length\n        val map1 = HashMap<Pair<Int,Int>, MutableList<Int>>()\n        val map2 = HashMap<Pair<Int,Int>, MutableList<Int>>()\n        var word = 0\n        var len = 0\n        var rez : MutableList<String> = mutableListOf()\n        for (i in 0 until len1) {\n            val ch = s1[i]\n            if (ch ==\' \') {\n                map1.computeIfAbsent(Pair(len,word)) { mutableListOf() }.add(i)\n                word = 0\n                len = 0\n            }\n            else {\n                word = word * 29 +(ch-\'a\'+1)\n                len++\n            }\n        }\n        map1.computeIfAbsent(Pair(len,word)) { mutableListOf() }.add(len1)\n        len = 0\n        word = 0\n        for (i in 0 until len2) {\n            val ch = s2[i]\n            if (ch ==\' \') {\n                map2.computeIfAbsent(Pair(len,word)) { mutableListOf() }.add(i)\n                len = 0\n                word = 0\n            }\n            else {\n                word = word * 29 +(ch-\'a\'+1)\n                len++\n            }\n        }\n        map2.computeIfAbsent(Pair(len,word)) { mutableListOf() }.add(len2)\n        for ((key,set) in map1) {\n            if (set.size==1 && !map2.containsKey(key)) {\n                val pos_end = set[0]\n                rez.add(s1.substring(pos_end-key.first,pos_end))\n            }\n        }\n        for ((key,set) in map2) {\n            if (set.size==1 && !map1.containsKey(key)) {\n                val pos_end = set[0]\n                rez.add(s2.substring(pos_end-key.first,pos_end))\n            }\n        }\n//        len = rez.size\n//        val arr : Array<String> = Array(len,{""})\n//        for (i in 0 until len) arr[i]=rez[i]\n//        return arr\n\n        return rez.toTypedArray()\n    }\n}\n```\n```Ver2 []\nclass Solution {\n    fun uncommonFromSentences(s1: String, s2: String): Array<String> {\n        val len1 = s1.length\n        val len2 = s2.length\n        val map = HashMap<Pair<Int,Int>, MutableList<Int>>()\n        var word = 0\n        var len = 0\n        val rez = Stack<String>()\n        // var rez : MutableList<String> = mutableListOf()\n        for (i in 0 until len1) {\n            val ch = s1[i]\n            if (ch ==\' \') {\n                map.computeIfAbsent(Pair(len,word)) { mutableListOf() }.add(i)\n                word = 0\n                len = 0\n            }\n            else {\n                word = word * 29 +(ch-\'a\'+1)\n                len++\n            }\n        }\n        map.computeIfAbsent(Pair(len,word)) { mutableListOf() }.add(len1)\n        len = 0\n        word = 0\n        for (i in 0 until len2) {\n            val ch = s2[i]\n            if (ch ==\' \') {\n                map.computeIfAbsent(Pair(len,word)) { mutableListOf() }.add(1000+i)\n                len = 0\n                word = 0\n            }\n            else {\n                word = word * 29 +(ch-\'a\'+1)\n                len++\n            }\n        }\n        map.computeIfAbsent(Pair(len,word)) { mutableListOf() }.add(1000+len2)\n        for ((key,set) in map) {\n            if (set.size==1) {\n                val pos_end = set[0]\n                rez.push(if (pos_end<1000) s1.substring(pos_end-key.first,pos_end)\n                else s2.substring(pos_end-1000-key.first,pos_end-1000))\n            }\n        }\n        return rez.toTypedArray()\n    }\n}\n```
['Kotlin']