Swift Solution (Beats 100%)
Solution 1: Using Remainder Set\nIn this solution, we iterate through 1, 11, 111, ... and stop either when num = 0 in which case num % K == 0 or we stop when we
**Solution 1: Using Remainder Set**\nIn this solution, we iterate through `1, 11, 111, ...` and stop either when `num = 0` in which case `num % K == 0` or we stop when we have seen `num` before. This means that  there is no number for which `num % K == 0` so we should return -1. \n```swift \nfunc smallestRepunitDivByK(_ K: Int) -> Int {\n    var length = 0 \n    var num = 0\n    var seen = Set<Int>()\n\n    repeat {\n        seen.insert(num)  \n        num = ((num * 10) + 1) % K \n        length += 1\n    } while num != 0 && !seen.contains(num)\n\n    return (num == 0) ? length : -1\n}\n```\n\n**Solution 2: Pigeonhole Principle**\nThere are `k` possible remainders for `num % k`. For example, if `k = 2` the possible remainders are `0, 1`. Therefore, by the pigeonhole principle, after performing `k` iterations either a remainder will repeat or we will find a number such that `num % k == 0`. \n```swift \nfunc smallestRepunitDivByK(_ K: Int) -> Int {\n    var length = 0 \n    var num = 0\n\n    for _ in 0 ..< K {\n        num = ((num * 10) + 1) % K \n        length += 1\n        if num == 0 { return length }\n    }\n\n    return -1\n}\n```\n\n**Solution 3: Performance Improvement**\nWe will combine solution 2 with a speed up covering lots of `K`. If the given `K` is divisible by either 2 or 5, then the solution will be `-1`. This is because dividing a number ending with 1 by a multiple of 2 or 5 will always have a remainder of 1.\n```swift \nfunc smallestRepunitDivByK(_ K: Int) -> Int {\n    if K % 2 == 0 || K % 5 == 0 { return -1 }\n    var length = 0 \n    var num = 0\n\n    for _ in 0 ..< K {\n        num = ((num * 10) + 1) % K \n        length += 1\n        if num == 0 { return length }\n    }\n\n    return -1\n}\n```\n
[]