All Substring Help needed

I am getting partially correct answer. Can anybody please tell me what case I am missing?
Thanks in advance!
My Code Link: CodeChef: Practical coding for everyone

Your solution fails for S=“tvr” and R=“ttvr”.
Correct answer for this test case is “ttvr” but code outputs “tvrt”.

1 Like

thank you harry_game

1 Like

Where do you think mine goes wrong?

The strings “s” and “r” can be 1e5 digits long… so try using long in place of int…

Your code fails for S=“bc” and R=“abbc”.
Correct answer is “abbc” but your code shows “abcb”

Hey @harry_game it would be great if you could share you thought process behind coming up with these corner cases…

I tried like this:
Suppose two strings are R and S,initialise ans="" then finding the characters in R which are smaller than S[0] and storing in ans after that pushing the S in the ans after that storing all left characters in the ans in smallest lexicographical order,but i m getting wrong answer .can anybody explain what is wrong with this logic and how to come up to generalised solution😂

1 Like

where did i go wrong?
link : CodeChef: Practical coding for everyone

Why is this giving segmentation error?
https://www.codechef.com/viewsolution/26848720

It is giving correct answers, in all the above test cases. Can someone please tell me, why is it giving wrong answer on submission?

t=int(input())
for _ in range(t):
  s=list(input().strip())
  r=list(input().strip())
  d={}
  f={}
  o=0
  for i in s:
    try:
      d[i]+=1
    except Exception:
      d[i]=1
  #print(d)
  for i in r:
    try:
      f[i]+=1
    except Exception:
      f[i]=1
  #print(f)
  for x in d:
    try:
      if(d[x]>f[x]):
        o=1
        break
      else:
        f[x]-=d[x]
    except Exception:
      o=1
      break
  if o==1:
    print("Impossible")
  else:
    l=[]
    for x in f:
      for h in range(f[x]):
        l.append(x)
    l.append(''.join(s))
    l=sorted(l)
    print(''.join(l))

that will not always work. you have to take care of equality with s[0]. consider two examples
suppose s=“bc” and r=“aabbc”
then ans must be “aabbc”
now suppose s=“ba” and r=“aabba”
then ans must be “aabab”