[Community puzzle] KGood

Hi Guys,

I am solving the puzzle and for some of the test cases I do not see a possible solution.
For instance it seems that for test 4 the highest number of consecutive characters is 3 - “ttt”, and I do not understand how counting to 18 in any direction can result in the right answer which is 46.
Can anybody please clarify the puzzle statement a bit.
Thanks in advance.

The puzzle is after the longest substring that contains at most K unique letters (eg if K was 26 then it would always be the entire string). There isn’t any mention of consecutive identical characters.

1 Like

Thanks for the clarification.
I misinterpreted the statement, counting the length of the substring resulting after K unique characters are encountered and aborting afterwards.
It works as expected now.

Hi, not so sure why but my solution works on all test cases but fails on validator 1 once submitted…

The input string of Validator 1 consists of a character repeated several times. Check whether your solution handles that scenario correctly.

1 Like

nice, thanks for the tip, that solved my issue !

Seems like I independently invented the clearly widely-known optimal method (though less elegantly and succinctly than some other solutions).

Please indicate a convenient time for you for an interview for the position of Lead Programmer in the High-Load Systems Development and Optimization Department.

Sincerely, Google HR Department.

P.S. what is your O(n)?

The constraints do not specify that the string consists of lowercase letters only.

1 Like

It actually does …

A string is called KGood if and only if it has no more than (ie. less than or equal to) K different characters (lowercase letters of English alphabet).

Also, O(n) means the complexity of your code : O(n) is a linear complexity, as it’s duration is directly proportionnal to the length of the input (it means it pass only once through the input).

I understand this sentence to mean that a string is good if it contains no more than K different lowercase letters, which does not actually exclude the presence of other symbols in it.

1 Like

This puzzle was nice, but I think the test cases leave a bit to be desired. To justify the “hard” classification, I think there should have been test cases that required a O(n) solution to avoid timeout.

1 Like

I completely agree. Although I solved it faster (>5min) than some puzzles in the Easy section. Many interesting puzzles lack test cases that require an optimal solution.

1 Like

Stumbling on the same problem of failing validator 1, I probably misinterpreted something but I can’t find a test failing on my code :cry:
I’ve tried multiples tests cases like “aaaaa” or “abbba” and my code return 5 or 3 which is ok I think for K=1 (for k>1 it return 5 for both cases).
I’d really appreciate a similar case as the validator to understand where my code fail as I really can not find the flaw actually.

Consider testing with higher values of k, and the previously mentioned condition remains satisfied:

Custom case

Try something where the repeated character length is equal to k:

zzzzzz
6
1 Like

Thanks a lot, I’ve found my bug. Your custom case was super helpfull

1 Like