3 ms·
Actually, the general answer came to me in about 5 seconds. I may have been lucky, though. The crux here is to notice that the order of the letters in the word
by loup-vaillant 5y ago
Actually, the general answer came to me in about 5 seconds. I may have been lucky, though.
The crux here is to notice that the order of the letters in the word we're given does not matter (because of the "any permutation" part). So it must be a matter of counting the letters. GlennS got it right: https://news.ycombinator.com/item?id=28550511 https://news.ycombinator.com/item?id=28550511
The second insight was that only the parity of occurrence mattered. Having 2 or 4 or 6 times the same letter is the same as not having the letter at all, so I only have to retain "zero" or "one" for each letter. Sets are perfect for this: "zero" means "not there", "one" means it's there, and at the end I can tolerate at most one odd letter.
- Aeolun 5y agoThat’s quite possible. And probably because you’d worked on something where this applied before. Also exactly the reason I posted my message since that doesn’t apply for everyone. People that already know may be blind to that though.