Skip to content

Instantly share code, notes, and snippets.

@bitstuffing
Last active September 16, 2022 21:44
Show Gist options
  • Save bitstuffing/38e770ccbb44836fe37795bd923e2944 to your computer and use it in GitHub Desktop.
Save bitstuffing/38e770ccbb44836fe37795bd923e2944 to your computer and use it in GitHub Desktop.
Gist for Sergio ;) answering to a question in his interview
'''
Gist for Sergio ;) answering to a question in his interview
You are given a string s and an array of strings words.
You should add a closed pair of bold tag <b> and </b> to wrap the substrings in s that exist in words.
If two such substrings overlap, you should wrap them together with only one pair of closed bold-tag.
If two substrings wrapped by bold tags are consecutive, you should combine them.
Example 1:
Input: s = "abcxyz123", words = ["abc","123"]
Output: "<b>abc</b>xyz<b>123</b>"
Example 2:
Input: s = "aaabbcc", words = ["aaa","aab","bc"]
Output: "<b>aaabbc</b>c"
Return s after adding the bold tags.
It's hard to describe, so let's code:
'''
import re
def letsDoIt(s,chars):
final = s
final2 = final
for word in chars:
counter = -1
target = ""
final = final2
open = -1
for i in range(0,len(final)):
target = final2[i:i+len(word)]
if "<" not in target and ">" not in target:
if counter >= 0:
counter += 1
if counter == len(word) and open>0:
#needs a fix in one detected case, so this if will check if there is an open <b> more than </b>
if len(re.findall('<b>', final2)) > len(re.findall('</b>', final2)) and i+len(word)>final2.rfind("<b>")+len("<b>"):
counter = -1
final2 = final2[:i]+"</b>"+final2[i:]
if target == word:
if counter == -1:
final2 = final2[:i]+"<b>"+final2[i:]
open = i
counter = 0
else: # needed two cases: if we read from left to right, so never will find a '<b>', just in case of '</b>'
if len(final2) > i+len(word)-1 and final2[i+len(word)-1] == '<':
temp = final2[i:]
temp = temp[:temp.find(">")+1+len(word)-1]
if word in temp.replace("</b>",""):
leftSize = len(temp.split("</b>")[0])
rightSize = len(word)-leftSize
temp = final2[:i]
temp2 = final2[i:i+leftSize]
temp3 = final2[i+leftSize+len("</b>"):i+leftSize+len("</b>")+rightSize]
temp3 += "</b>"+temp2[i+leftSize+len("</b>")+rightSize:]
temp4 = final2[i+len("</b>")+leftSize+rightSize:]
final2 = temp+temp2+temp3+temp4
elif word in temp[:temp.find("<b>")]+temp[temp.find("<b>")+len("<b>"):]:
leftSize = len(temp.split("<b>")[0])
rightSize = len(word)-leftSize
temp = final2[:i]+"<b>"
temp2 = final2[i:i+leftSize]
temp3 = final2[i+leftSize+len("<b>"):i+leftSize+len("<b>")+rightSize]
temp3 += temp2[i+leftSize+len("<b>")+rightSize:]
temp4 = final2[i+len("<b>")+leftSize+rightSize:]
final2 = temp+temp2+temp3+temp4
#put it for and when len(word) < index
'''
if len(re.findall('<b>', final2))+1==len(re.findall('</b>', final2)) and final2.rfind("<b>") >= final2.rfind("</b>") and final2.rfind("<b>")+len(word)>=i:
final2=final2[:final2.rfind("<b>")+len("<b>")+len(word)]+"</b>"+final2[final2.rfind("<b>")+len("<b>")+len(word):]
'''
#it's the same, done at different way
if len(re.findall('<b>', final2)) > len(re.findall('</b>', final2)) and i+len(word)>final2.rfind("<b>")+len("<b>"):
if i-open==len(word):
temp = final2[:final2.rfind("<b>")+len("<b>")+len(word)]
temp2 = final2[final2.rfind("<b>")+len("<b>")+len(word):]
final2 = temp+"</b>"+temp2
#first replace is because requirements of the exercise say that
#sorry for the second, time issues (just 15 minutes) in elif of special cases sometimes it writes two <b>, because I don't have time and simply just need a check if temp ends with <b> or not
return final2.replace("</b><b>","").replace("<b><b>","")
if __name__ == "__main__":
#s = "abcxyz123aaaaaaaaaa"
#chars = ["abc","123"]
s = "aaaabbcc"
chars = ["aaa","bc","aab"]
print(letsDoIt(s,chars))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment