一道面试题目
有n个字符的字符串a 然后有两个m长的字符串bc,b中的第一个字符如果在a中出现就用c中的第一个替换,第2345。。。。。个同理
要求:o(n)
以下是 phyton实现
def replaceString(a,b,c):
letterList = {}
tmp = 97
i = 0
k = 0
l = 0
myList = []
while i < 26:
letterList.setdefault(chr(tmp))
letterList[chr(tmp)] = 0
tmp = tmp + 1
i = i + 1
for j in a:
letterList[j] = 1
while k < len(b):
if letterList[b[k]] == 1:
myList.append(c[k])
else:
myList.append(b[k])
k = k + 1
print a
print b
print c
while l < len(myList):
print myList[l],
l = l + 1
a = "asdfgh"
b = "bgu"
c = "nkm"
replaceString(a,b,c)
def replaceString(a,b,c):
letterList = {}
tmp = 97
i = 0
k = 0
l = 0
myList = []
while i < 26:
letterList.setdefault(chr(tmp))
letterList[chr(tmp)] = 0
tmp = tmp + 1
i = i + 1
for j in a:
letterList[j] = 1
while k < len(b):
if letterList[b[k]] == 1:
myList.append(c[k])
else:
myList.append(b[k])
k = k + 1
print a
print b
print c
while l < len(myList):
print myList[l],
l = l + 1
a = "asdfgh"
b = "bgu"
c = "nkm"
replaceString(a,b,c)