maniratnagupta's blog

By maniratnagupta, history, 7 years ago, In English

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

  • Vote: I like it
  • -20
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

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 years ago, # |
Rev. 6   Vote: I like it +3 Vote: I do not like it

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.

  • »
    »
    7 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    Thanks for the help I understood it crisp and clear Can you add me as a friend?