Блог пользователя maniratnagupta

Автор maniratnagupta, история, 7 лет назад, По-английски

why my code is wrong? i'm just curious where the logic went wrong it failed at test case:6 Your text to link here... Please help me ASAP

  • Проголосовать: нравится
  • -20
  • Проголосовать: не нравится

»
7 лет назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

You fail to consider the case where there's more than one of the same character. For example, in the test case "www", your program says "chat with her", but the answer should be "ignore him". After count-=1, you should do "if (count>0) count=1;"

»
7 лет назад, # |
Rev. 6   Проголосовать: нравится +3 Проголосовать: не нравится

1) When i is equal to j, n (the j-th character) will always be equal to c[i]. Try starting your for at i = j + 1.

2) You need to break out of the for loop when n == c[i] is true, in order to not count repeating characters multiple times.

3) Because you are already subtracting c.Length from count (character by character when doing count = count-1), count after the for loop is exactly the number of distinct characters, but with a minus sign. There are two ways to fix the calculation of k, the number of distinct characters: Either delete the count = count-1 line (and keep k = c.Length - count, because count would then represent the number of repeated letters) or use k = -count.

I submitted your solution with the proposed changes here.