I am trying to apply the distributive law for two strings, e.g. "ab+ac" returns "a(b+c)". I managed to do that. But the problem I am facing is with longer texts. e.g. "aabbcd+aabbgf" it should return "aabb(cd+gf)". Where is the problem in my code?
inputString = input("Enter Regular Expression: ")
def destributionOfAnExpression():
if "+" not in inputString:
return inputString
else:
h = inputString.split("+")
y = h[0]
z = h[1]
similar = ""
for i in y:
for j in z:
while i==j:
similar+=i
break
break
print(similar+"("+y.strip(similar)+"+"+z.strip(similar)+")")