1、用Python实现栈
class Stack(object):
def __init__(self):
self.items = []
def isEmpty(self):
return len(self.items) == 0
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[-1]
def size(self):
return len(self.items)
2、异序词检测
如果一个字符串只是重排了另一个字符串的字符,那么这个字符串就是另一个的异序词,比如heart和earth
计数法:判断两个字符串中每个字符出现的次数是否相等
def solution(s1, s2):
c1 = [0] * 26
c2 = [0] * 26
for i in range(len(s1)):
pos = ord(s1[i]) - ord('a')
c1[pos] += 1
for i in range(len(s2)):
pos = ord(s2[i]) - ord('a')
c2[pos] += 1
stillok = True
for i in range(26):
if c1[i] != c2[i]:
stillok = False
break
return stillok