字符串
字符串
一.如何求一个字符串的所有排列
1.递归法
def Swap(str,i,j):
tmp=str[i]
str[i]=str[j]
str[j]=tmp
def Permutation(str,start):
if str==None or start<0:
return
if start==len(str)-1:
print("".join(str),end=" ")
else:
i=start
while i<len(str):
Swap(str,start,i)
Permutation(str,start+1)
Swap(str,start,i)
i+=1
def Permutation_transe(s):
str=list(s)
Permutation(str,0)
if __name__=="__main__":
s="abc"
Permutation_transe(s)
abc acb bac bca cba cab
二.如何求两个字符串的最长公共子串
1.动态规划法
def GetMaxSubStr(str1,str2):
length1=len(str1)
length2=len(str2)
sb=""
maxs=0
maxI=0
M=[([None]*(length1+1)) for i in range(length2+1)]
i=0
while i<length1+1:
M[i][0]=0
i+=1
j=0
while j<length2+1:
M[0][j]=0
j+=1
i=1
while i<length1+1:
j=1
while j<length2+1:
if list(str1)[i-1]==list(str2)[j-1]:
M[i][j]=M[i-1][j-1]+1
if M[i][j]>maxs:
maxs=M[i][j]
maxI=i
else:
M[i][j]=0
j+=1
i+=1
i=maxI-maxs
while i<maxI:
sb=sb+list(str1)[i]
i+=1
return sb
if __name__=="__main__":
str1="abccade"
str2="dgcadde"
print(GetMaxSubStr(str1,str2))
cad
def GetMaxSubStr(str1,str2):
length1=len(str1)
length2=len(str2)
maxLen=0
tmpMaxLen=0
maxLenEnd=0
sb=""
i=0
while i<length1+length2:
s1begin=s2begin=0
tmpMaxLen=0
if i<length1:
s1begin=length1-i
else:
s2begin=i-length1
j=0
while (s1begin+j<length1) and (s2begin+j<length2):
if list(str1)[s1begin+j]==list(str2)[s2begin+j]:
tmpMaxLen+=1
else:
if tmpMaxLen>maxLen:
maxLen=tmpMaxLen
maxLenEnd=s1begin+j
else:
tmpMaxLen=0
j+=1
if tmpMaxLen>maxLen:
maxLen=tmpMaxLen
maxLenEnd=s1begin+j
i+=1
i=maxLenEnd-maxLen
while i<maxLenEnd:
sb=sb+list(str1)[i]
i+=1
return sb
if __name__=="__main__":
str1="abccade"
str2="dgcadde"
print(GetMaxSubStr(str1,str2))
cad
三.如何对字符串进行反转
1.临时变量法
def ReverseStr(str):
ch=list(str)
length=len(ch)
i=0
j=length-1
while i<j:
tmp=ch[i]
ch[i]=ch[j]
ch[j]=tmp
i+=1
j-=1
return "".join(ch)
if __name__=="__main__":
str="abcdefg"
print("字符串"+str+"翻转后为:"+ReverseStr(str))
字符串abcdefg翻转后为:gfedcba
2.直接交换法
def ReverseStr(str):
ch=list(str)
length=len(ch)
i=0
j=length-1
while i<j:
ch[i]=chr(ord(ch[i])^ord(ch[j]))
ch[j]=chr(ord(ch[i])^ord(ch[j]))
ch[i]=chr(ord(ch[i])^ord(ch[j]))
i+=1
j-=1
return "".join(ch)
if __name__=="__main__":
str="abcdefg"
print("字符串"+str+"翻转后为:"+ReverseStr(str))
字符串abcdefg翻转后为:gfedcba
引申:
def ReverseStr(ch,front,end):
while front<end:
ch[front]=chr(ord(ch[front])^ord(ch[end]))
ch[end]=chr(ord(ch[front])^ord(ch[end]))
ch[front]=chr(ord(ch[front])^ord(ch[end]))
front+=1
end-=1
def SwapWords(str):
length=len(str)
ch=list(str)
ReverseStr(ch,0,length-1)
begin=0
i=1
while i<length:
if ch[i]==" ":
ReverseStr(ch,begin,i-1)
begin=i+1
i+=1
ReverseStr(ch,begin,length-1)
return "".join(ch)
if __name__=="__main__":
str="how are you"
print("字符串"+str+"翻转后为:"+SwapWords(str))
字符串how are you翻转后为:you are how
四.如何判断两个字符串是否为换位字符串
def Compare(s1,s2):
result=True
bCount=[None]*256
i=0
while i<256:
bCount[i]=0
i+=1
i=0
while i<len(s1):
bCount[ord(list(s1)[i])-ord('0')]+=1
i+=1
i=0
while i<len(s2):
bCount[ord(list(s2)[i])-ord('0')]-=1
i+=1
i=0
while i<256:
if bCount[i]!=0:
result=False
break
i+=1
return result
if __name__=="__main__":
str1="aaaabbc"
str2="abcbaaa"
print(str1+"和"+str2)
if Compare(str1,str2):
print("是换位字符")
else:
print("不是换位字符")
aaaabbc和abcbaaa
是换位字符
五.如何判断两个字符串的包含关系
def IsContain(str1,str2):
length1=len(str1)
length2=len(str2)
if length1<length2:
i=0
while i<length1:
j=0
while j<length2:
if list(str1)[i]==list(str2)[j]:
break
j+=1
if j>=length2:
return False
i+=1
else:
i=0
while i<length2:
j=0
while j<length1:
if list(str1)[j]==list(str2)[i]:
break
j+=1
if j>=length1:
return False
i+=1
return True
if __name__=="__main__":
str1="abcdef"
str2="acf"
isContain=IsContain(str1,str2)
print(str1+"与"+str2)
if isContain:
print("有包含关系")
else:
print("没有包含关系")
abcdef与acf
有包含关系
def IsContain(s1,s2):
k=0
flag=[None]*52
i=0
while i<52:
flag[i]=0
i+=1
count=0
length1=len(s1)
length2=len(s2)
if length1<length2:
shortStr=s1
minLen=length1
longStr=s2
maxLen=length2
else:
shortStr=s2
minLen=length2
longStr=s1
maxLen=length1
i = 0
while i < minLen:
if ord(list(shortStr)[i]) >= ord('A') and ord(list(shortStr)[i]) <= ord('Z'):
k = ord(list(shortStr)[i]) - ord('A')
else:
k = ord(list(shortStr)[i]) - ord('a') + 26
if flag[k] == 0:
flag[k] = 1
count +=1
i += 1
j=0
while j<maxLen:
if ord(list(longStr)[j])>=ord('A') and ord(list(longStr)[j])<=ord('Z'):
k=ord(list(longStr)[j])-ord('A')
else:
k=ord(list(longStr)[j])-ord('a')+26
if flag[k]==1:
flag[k]=0
count-=1
if count==0:
return True
j+=1
return False
if __name__=="__main__":
str1="abcdef"
str2="acf"
isContain=IsContain(str1,str2)
print(str1+"与"+str2)
if isContain:
print("有包含关系")
else:
print("没有包含关系")
abcdef与acf
有包含关系
六.如何对由大小写字母组成的字符数组排序
def ReverseArray(ch):
length=len(ch)
begin=0
end=length-1
while begin<end:
while ch[begin]>='a' and ch[end]<='z' and end>begin:
begin+=1
while ch[end]>='A' and ch[end]<='Z' and end>begin:
end-=1
tmp=ch[begin]
ch[begin]=ch[end]
ch[end]=tmp
if __name__=="__main__":
ch=list("AbcDef")
ReverseArray(ch)
i=0
while i<len(ch):
print(ch[i],end="")
i+=1
fbceDA
七.如何消除字符串的内嵌括号
def RemoveNestedPare(strs):
if strs==None:
return strs
Parentheses_num=0
if list(strs)[0]!="(" or list(strs)[-1]!=")":
return None
sb="("
i=1
while i<len(strs)-1:
ch=list(strs)[i]
if ch=="(":
Parentheses_num+=1
elif ch==")":
Parentheses_num-=1
else:
sb=sb+(list(strs)[i])
i+=1
if Parentheses_num!=0:
print("括号不匹配")
return None
sb=sb+")"
return sb
if __name__=="__main__":
strs="(1,(2,3),(4,(5,6),7))"
print(strs+"去除嵌套括号后为:"+RemoveNestedPare(strs))
(1,(2,3),(4,(5,6),7))去除嵌套括号后为:(1,2,3,4,5,6,7)
九.如何实现字符串的匹配
def Match(s,p):
if s==None or p==None:
print("参数不合理")
return -1
slen=len(s)
plen=len(p)
if slen<plen:
return -1
i=0
j=0
while i<slen and j<plen:
if list(s)[i]==list(p)[j]:
i+=1
j+=1
else:
i=i-j+1
j=0
if i>slen-plen:
return -1
if j>=plen:
return i-plen
return -1
if __name__=="__main__":
s="xyzabcd"
p="abc"
print(Match(s,p))
3
十.如何求字符串里的最长回文子串
class Test:
def __init__(self):
self.startIndex=None
self.length=None
def getStartIndex(self):
return self.startIndex
def getLen(self):
return self.length
def getLongestPalindrome(self,strs):
if strs==None:
return
n=len(strs)
if n<1:
return
self.startIndex=0
self.length=1
historyRecord=[([None]*n) for i in range(n)]
i=0
while i<n:
j=0
while j<n:
historyRecord[i][j]=0
j+=1
i+=1
i=0
while i<n:
historyRecord[i][i]=1
i+=1
i=0
while i<n-1:
if list(strs)[i]==list(strs)[i+1]:
historyRecord[i][i+1]=1
self.startIndex=i
self.length=2
i+=1
pLen=3
while pLen<=n:
i=0
while i<n-pLen+1:
j=i+pLen-1
if list(strs)[i]==list(strs)[j] and historyRecord[i+1][j-1]==1:
historyRecord[i][j]=1
self.startIndex=i
self.length=pLen
i+=1
pLen+=1
if __name__=="__main__":
strs="abcdefgfedxyz"
t=Test()
t.getLongestPalindrome(strs)
if t.getStartIndex()!=-1 and t.getLen()!=1:
print("最长的回文字串:")
i=t.getStartIndex()
while i<t.getStartIndex()+t.getLen():
print(list(strs)[i],end="")
i+=1
else:
print("查找失败")
最长的回文字串:
defgfed