Get Longest Substring in Python
def main():
print()
exp = input("Enter a math expression: ")
stack = []
for c in exp:
if c == "(":
stack.append(c)
elif c == ")":
if stack:
stack.pop()
else:
# More closing than opening → invalid
print("The math expression is invalid.")
return
if not stack:
print("The math expression is valid.")
else:
print("The math expression is invalid.")
if __name__ == "__main__":
main()