```
def lengthOfLongestSubstringTwoDistinct(self, s):
size = len(s)
start = 0
ans = 0
left_most = {} #continuous_left_most
for i in xrange(size):
if s[i] not in left_most:
if len(left_most) == 2:
start = left_most[s[i-1]]
for k in left_most: # only have 2 key
if k != s[i-1]:
tmp = k
break
left_most.pop(tmp)
left_most[s[i]] = i
else:
if s[i] != s[i-1]:
left_most[s[i]] = i
ans = max(ans, i-start+1)
return ans
```