mohitgupta943's blog

By mohitgupta943, history, 13 days ago, In English,

I am solving This Question this question. I tried...below is my code. I am not able to find where it is getting wrong.Kindly help

s=input()
cnt=[0 for i in range(26)]
for i in range(len(s)):
    cnt[ord(s[i])-97]+=1
b=[]
ans=""
for i in range(len(s)):

    x=s[i]
    bol=False
    for j in range(ord(x)-97):
        if cnt[j]!=0:
            b.append(x)
            cnt[ord(x)-97]-=1
            bol=True
            break
    if bol==False:
        ans+=x
        cnt[ord(x)-97]-=1
   
   # print(ord(x),b,ans)
if len(b)>0:
    while(len(b)>0):
        x=b[len(b)-1]
        ans+=x
        b.pop()
print(ans)
 
 
 
 
  • Vote: I like it
  • -16
  • Vote: I do not like it

»
13 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by mohitgupta943 (previous revision, new revision, compare).

»
13 days ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by mohitgupta943 (previous revision, new revision, compare).

»
13 days ago, # |
  Vote: I like it +3 Vote: I do not like it

Two things:

  1. Instead of copy-pasting your code, at least try to explain what the idea of your solution is. Maybe your idea is wrong. No one wants to spend 10 minutes reading your code just to understand what you are trying to do. The problems are about thinking and ideas, the fact that you have to write code is just a side effect.

  2. How have you tried to debug this? Did you do the following:

  • make a "stupid" solution that just tries all possible orders of doing the operations;
  • make a program to generate many small test cases;
  • compare the outputs of the stupid solution and your solution.
  • »
    »
    9 days ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks @-is-this-fft-, I would take care of it from next time..Thanks for your guidance