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.
I wrote this recursive solution that is quite nice, but I am getting runtime errors on tests 12, 13, 14. Can someone spot the issue? I've noticed one of the misidentified examples was:
If you try runnning this code with that custom example, you will get a runtime error. I can't figure out what it is because every thing seems to be running as expected when I add print statements. Although, the runtime error occurs in one of the first few recursive calls.
# Recursive helper function for abbreviationdefhelper(a:str,b:str,n:int,m:int,res:str='')->str:# if we have reached the end of a, return "YES" if our result equals our target, b# else return "NO"ifn==len(a):return"YES"ifres==belse"NO"# if index pointer for b is less than its length, recursively add parts of aifm<len(b):# if the current result string is not equal to b up until this moment, return "NO"ifres!=b[:m]:return"NO"# get the uppercase version of the current letter in aup=a[n].upper()# if value in a is capitalized, add it to the result string, and increase the index pointers# return whether or not the substring a[n+1:] is an abbreviationifa[n].isupper():returnhelper(a,b,n+1,m+1,res+a[n])# else if the upper case version equals the current character in b, # return result of two cases:# 1. add the upper cased value to result and increment the index pointers# 2. skip this value in a and try using the next value for comparing with b elifup==b[m]:return"YES"ifany(result=="YES"forresultin[helper(a,b,n+1,m+1,res+up),helper(a,b,n+1,m,res)])else"NO"# else if we have reached the end of b, only continue adding characters of a if they are# already capitalizedelifa[n].isupper():returnhelper(a,b,n+1,m,res+a[n])# if we have reached the end of b and current pointer of is not a capital, keep moving pointer# of a to the rightreturnhelper(a,b,n+1,m,res)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Abbreviation
You are viewing a single comment's thread. Return to all comments →
I wrote this recursive solution that is quite nice, but I am getting runtime errors on tests 12, 13, 14. Can someone spot the issue? I've noticed one of the misidentified examples was:
1 hHhAhhcahhacaccacccahhchhcHcahaahhchhhchaachcaCchhchcaccccchhhcaahhhhcaacchccCaahhaahachhacaahhaachhhaaaCalhhchaccaAahHcchcazhachhhaaahaahhaacchAahccacahahhcHhccahaachAchahacaahcahacaahcahacaHhccccaahaahacaachcchhahhacchahhhaahcacacachhahchcaAhhcaahchHhhaacHcacahaccccaaahacCHhChchhhahhchcahaaCccccahhcaachhhacaaahcaaaccccaacaaHachaahcchaahhchhhcahahahhcaachhchacahhahahahAahaAcchahaahcaaaaahhChacahcacachacahcchHcaahchhcahaachnachhhhcachchahhhacHhCcaHhhhcaCccccaaahcahacahchahcaachcchaachahhhhhhhhcahhacacCcchahccaaaaaHhhccaAaaaCchahhccaahhacaccchhcahhcahaahhgacahcahhchcaaAccchahhhaahhccaaHcchaccacahHahChachhcaaacAhacacaacacchhchchacchchcacchachacaahachccchhhaccahcacchaccaahaaaccccccaaaaaaaHhcahcchmcHchcchaaahaccchaaachchHahcaccaaccahcacacahAhaacaacaccaccaaacahhhcacAhaCchcaacCcccachhchchcchhchahchchahchchhchcacaachahhccacachaAhaaachchhchchchhaachahaahahachhaaaccacahhcacchhhaaachaaacAahhcachchachhhcacchacaaChCahhhccahChaachhcahacchanaaacchhhccacacchcahccchAcahacaaachhacchachccaaHacaacAhahcCh HAHHCHAACCCAHCHHAHHAHCACCHCCHHCAAHHCACCCAHHHACAAHHHHCHHCAHHAHHAAAHAACAAHAHHCAHAHACHACHCHACACHAAHHAAAHCAHHACACAACHHHCHAHCAHCHHHAHAHACCAAAHCHHCHHCCAACCCCAACHACAACAAHACHCHAHHACCHCAHHHAAACHACAACHCACACAHHCCHAHACCCACCAACHCHHHCCCCCHCCAHHCAAHHAHHHHHHHAACCCCAHCCAAAAAHHHAAAACCAHHCAHACACCHHCHAHAHHCHAACHHHHHCCHCCAHAHCHCAAACCACCCCHACCACHHACHHACACHACCAACCCCAAAAHHAHCHHHCCAHCCHACHHAHCCACACCHAHAAACACCCCAHCCAHACCCCCCHCCHHCHHHHCHCHCAHHHACHAHAACCCAAAACHAACAAAHHAAHAAAHACHHCACHCCHCHAACHACACHHCCCCCAHCACHAAAHCHCAHACAAC
If you try runnning this code with that custom example, you will get a runtime error. I can't figure out what it is because every thing seems to be running as expected when I add print statements. Although, the runtime error occurs in one of the first few recursive calls.