1. 程式人生 > >[Data Structure] An Algorithm for Matching Delimiters

[Data Structure] An Algorithm for Matching Delimiters

struct ces ons false str can turn class !=

An important task when processing arithmetic expressions is to mach delimiters.

We can use Stack to solve this problem.

def is_matched(expr):
	left=‘({[‘
	right=‘)}]‘

	S=ArrayStack()

	for c in expr:
		if c in left:
			S.push(c)
		elif c in right:
			if S.is_empty():
				return False
			if right.index(c)!=left.index(S.pop()):
				return False
	return S.is_empty()

  

[Data Structure] An Algorithm for Matching Delimiters