class Solution(object):
def reverseStr(self, s, k):
"""
:type s: str
:type k: int
:rtype: str
"""
lst = []
for i in xrange(0, len(s), 2*k):
lst.append(s[i:i+2*k])
for i, s in enumerate(lst):
lst[i] = s[:k][::-1]+s[k:]
return ''.join(lst)


class Solution(object):
def findMinDifference(self, timePoints):
"""
:type timePoints: List[str]
:rtype: int
"""
def get(x):
return int(x[:2])*60+int(x[3:])
tp = sorted(map(get, timePoints))
ans = 2 ** 31
for i in xrange(len(tp)-1):
ans = min(ans, tp[i+1]-tp[i])
ans = min(ans, get('24:00')-tp[-1]+tp[0])
return ans


# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
def str2tree(self, s):
"""
:type s: str
:rtype: TreeNode
"""
if not s:
return None
def build(s):
i = j = 0
while i < len(s) and s[i] != '(':
i += 1
root = TreeNode(int(s[:i]))
j = i
if i < len(s):
c = 1
j = i + 1
while j < len(s) and c != 0:
if s[j] == '(':
c += 1
elif s[j] == ')':
c -= 1
j += 1
root.left = build(s[i+1:j-1])
i = j
if i < len(s):
root.right = build(s[i+1:len(s)-1])
return root

return build(s)


1. 以第一个字符为前缀，然后紧接略去的字符串个数，以字符串最后一个字符结尾；
2. 如果存在不同的字符串却有一样的缩写，那么要以前两个字符为前缀，以此类推，直到所有字符串都有唯一的缩写；
3. 如果缩写并不能减少字符串的长度，那么就保持原来字符串。

["like", "god", "internal", "me", "internet", "interval", "intension", "face", "intrusion"]

["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"]

from collections import defaultdict

class Solution(object):
def wordsAbbreviation(self, dict):
"""
:type dict: List[str]
:rtype: List[str]
"""
n = len(dict)
ans = [''] * n
visited = [False] * n
k = 1
while n > 0:
tmp = defaultdict(list)
for i, w in enumerate(dict):
if visited[i]:
continue
if k + 2 >= len(w):
ans[i] = w
n -= 1
visited[i] = True
else:
tmp[w[:k]+str(len(w)-k-1)+w[-1]].append(i)
for t, v in tmp.items():
if len(v) == 1:
ans[v[0]] = t
visited[v[0]] = True
n -= 1
k += 1
return ans