Python O(K) with Detailed Explanations
For a given K, we evaluate 1 % K, 11 % K, 111 % K, ..., 11...1 (K \'1\'s) % K. \n If any remainder is 0, then the current number is the smallest integer divisib
For a given K, we evaluate `1 % K, 11 % K, 111 % K, ..., 11...1 (K \'1\'s) % K`. \n* If any remainder is 0, then the current number is the smallest integer divisible by `K`. \n* If none of the remainders is 0, then at some point, there must be some duplicate remainders (due to [Pigeonhole principle](https://en.wikipedia.org/wiki/Pigeonhole_principle)), as the `K` remainders can only take at most `K-1` different values excluding 0. In this case, no number with the pattern 1...1 is divisible by `K`. This is because once a remainder has a duplicate, the next remainder will be in a loop, as the previous remainder determines the next_mod, i.e., `next_mod = (10 * prev_mod + 1) % K`. Therefore, we will never see remainder==0.\n\nA simple example is when K is 6. Once we see 1111 % 6 = 1, we immediately know 11111 % 6 will be 5, since 1 % 6 = 1 and 11 % 6 = 5. Therefore, there will be no such number that is divisible by 6.\n* 1 % 6 = 1\n* 11 % 6 = 5\n* 111 % 6 = 3\n* 1111 % 6 = 1\n* 11111 % 6 = 5\n* 111111 % 6 = 3\n\nAlso, it is easy to see that for any number whose last digit is not in `{1, 3, 7, 9}`, we should return `-1`.\n```\nclass Solution:\n    def smallestRepunitDivByK(self, K: int) -> int:\n        if K % 10 not in {1, 3, 7, 9}: return -1\n        mod, mod_set = 0, set()\n        for length in range(1, K + 1):\n            mod = (10 * mod + 1) % K\n            if mod == 0: return length\n            if mod in mod_set: return -1\n            mod_set.add(mod)\n        return -1\n```
['Hash Table', 'Python']