We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
LenaElika’s Python-fu is very strong. However, I’m afraid there is a flaw in her reasoning, which this case illustrates.
The code processes individual characters in descending order of count. However, we need to go in descending order of the sum of both counts in the pair, which will not necessarily happen. Let us consider this case.
Observe that n, p, x, and y each occur 3 times, and that ynynyn, ypypyp, and nxnxnx are valid solutions, for a correct answer of 6.
Using LenaElika’s algorithm, however, depending on the random order in which letters with the same count are used, we will sometimes get these valid answers, but sometimes get invalid answers such as zhzhz or zozoz. This happens when z is visited first. It seems like the algorithm, with this data, usually gives the correct answer, but once in a while it gives a wrong answer.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Two Characters
You are viewing a single comment's thread. Return to all comments →
LenaElika’s Python-fu is very strong. However, I’m afraid there is a flaw in her reasoning, which this case illustrates.
The code processes individual characters in descending order of count. However, we need to go in descending order of the sum of both counts in the pair, which will not necessarily happen. Let us consider this case.
Observe that
n
,p
,x
, andy
each occur 3 times, and thatynynyn
,ypypyp
, andnxnxnx
are valid solutions, for a correct answer of6
.Using LenaElika’s algorithm, however, depending on the random order in which letters with the same count are used, we will sometimes get these valid answers, but sometimes get invalid answers such as
zhzhz
orzozoz
. This happens whenz
is visited first. It seems like the algorithm, with this data, usually gives the correct answer, but once in a while it gives a wrong answer.