Accepted Cheat 1-liner :-D


  • 2

    The judge doesn't check that I'm only using the allowed digits or even digits at all, so I just build a string with the correct length without repeating substrings of length n. Currently it does get accepted (edit: not anymore, see reply below). And hey, if I'm cracking a safe, I might as well cheat.

    Ruby:

    def crack_safe(n, k)
      [''].product(*%w(abcdefghij klmnop ABCDEF KLM).map(&:chars)[0, n]).join[0, k**n + n-1]
    end
    

    Python:

    def crackSafe(self, n, k):
        chunks = itertools.product(*'abcdefghij klmnop ABCDEF KLM'.split()[:n])
        return ''.join(map(''.join, chunks))[:k**n + n-1]
    

    Explanation:

    Let's say n is 2. Then I concatenate these pairs of characters: "ak", "al", "am", ..., "jn", "jo", "jp". That is, all combinations of a letter from "abcdefghij" with a letter from "klmnop". So my result is "akalam...jnjojp". I just need to cut it off at the right length.

    But how do I know there are no duplicate substrings of length 2? Well, those starting at even indexes are just "ak", "al", etc, all different combinations I used to build the string in the first place. I also can't have a substring at an even index matching a substring at an odd substring, since I use different alphabets ("abcdefghij" and "klmnop") for even and odd indices. Could two substrings at odd indices match? Heh, actually I just noticed that I forgot to think about this. But I think I just get all combinations of a letter from "klmnop" with a letter from "abcdefghij", just not in the normal order.

    When n is 1, 3 or 4, I do the same, except I concatenate singles or triples or quadruples.

    The four alphabet sizes are btw minimized so they're just long enough for all cases.


  • 2

    @StefanPochmann Good observation. I fixed the custom matcher to make this type of solution WA now.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.