''' 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 and 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: "abcxyz123" Example 2: Input: s = "aaabbcc", words = ["aaa","aab","bc"] Output: "aaabbcc" 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 more than if len(re.findall('', final2)) > len(re.findall('', final2)) and i+len(word)>final2.rfind("")+len(""): counter = -1 final2 = final2[:i]+""+final2[i:] if target == word: if counter == -1: final2 = final2[:i]+""+final2[i:] open = i counter = 0 else: # needed two cases: if we read from left to right, so never will find a '', just in case of '' 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("",""): leftSize = len(temp.split("")[0]) rightSize = len(word)-leftSize temp = final2[:i] temp2 = final2[i:i+leftSize] temp3 = final2[i+leftSize+len(""):i+leftSize+len("")+rightSize] temp3 += ""+temp2[i+leftSize+len("")+rightSize:] temp4 = final2[i+len("")+leftSize+rightSize:] final2 = temp+temp2+temp3+temp4 elif word in temp[:temp.find("")]+temp[temp.find("")+len(""):]: leftSize = len(temp.split("")[0]) rightSize = len(word)-leftSize temp = final2[:i]+"" temp2 = final2[i:i+leftSize] temp3 = final2[i+leftSize+len(""):i+leftSize+len("")+rightSize] temp3 += temp2[i+leftSize+len("")+rightSize:] temp4 = final2[i+len("")+leftSize+rightSize:] final2 = temp+temp2+temp3+temp4 #put it for and when len(word) < index ''' if len(re.findall('', final2))+1==len(re.findall('', final2)) and final2.rfind("") >= final2.rfind("") and final2.rfind("")+len(word)>=i: final2=final2[:final2.rfind("")+len("")+len(word)]+""+final2[final2.rfind("")+len("")+len(word):] ''' #it's the same, done at different way if len(re.findall('', final2)) > len(re.findall('', final2)) and i+len(word)>final2.rfind("")+len(""): if i-open==len(word): temp = final2[:final2.rfind("")+len("")+len(word)] temp2 = final2[final2.rfind("")+len("")+len(word):] final2 = temp+""+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 , because I don't have time and simply just need a check if temp ends with or not return final2.replace("","").replace("","") if __name__ == "__main__": #s = "abcxyz123aaaaaaaaaa" #chars = ["abc","123"] s = "aaaabbcc" chars = ["aaa","bc","aab"] print(letsDoIt(s,chars))