summaryrefslogtreecommitdiff
path: root/progs/a749.py
blob: 9c4c559e0249bbd246e3df3a06229ce2c6a72763 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def swap_count(s):
	chars = s
	count_left = 0
	count_right = 0
	swap = 0
	imbalance = 0; 
	for i in range(len(chars)):
		if chars[i] == '[':
			count_left += 1
			if imbalance > 0:
				swap += imbalance
				imbalance -= 1
		elif chars[i] == ']':
			count_right += 1
			imbalance = (count_right - count_left) 
	return swap