├── .gitignore ├── strstr.py ├── compareString.py └── anagram.py /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | *.pyc 3 | *.txt 4 | *.log 5 | -------------------------------------------------------------------------------- /strstr.py: -------------------------------------------------------------------------------- 1 | #-*-coding=utf-8-*- 2 | __author__ = 'xda' 3 | def strstsrQ(source,target): 4 | for s in range(len(source)): 5 | cur=0 6 | for t in range(len(target)): 7 | if s+t>=len(source): 8 | return -1 9 | 10 | if target[t] == source[t+s]: 11 | cur=cur+1 12 | else: 13 | break 14 | if cur==(len(target)-1): 15 | return s 16 | 17 | return -1 18 | 19 | 20 | a='I like China!' 21 | b='China' 22 | c='adcde' 23 | d='e' 24 | print strstsrQ(c,d) 25 | -------------------------------------------------------------------------------- /compareString.py: -------------------------------------------------------------------------------- 1 | #-*-coding=utf-8-*- 2 | __author__ = 'xda' 3 | ''' 4 | 比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写字母 5 | 6 | 注意事项 7 | 8 | 在 A 中出现的 B 字符串里的字符不需要连续或者有序。 9 | 10 | 您在真实的面试中是否遇到过这个题? Yes 11 | 样例 12 | 给出 A = "ABCD" B = "ACD",返回 true 13 | 14 | 给出 A = "ABCD" B = "AABC", 返回 false 15 | ''' 16 | def compareString(A,B): 17 | if 0==len(A): 18 | return True 19 | 20 | if len(A)!=0 and len(B)==0: 21 | return False 22 | 23 | 24 | for i in A: 25 | if i not in B: 26 | return False 27 | 28 | return True 29 | 30 | A="A" 31 | B="" 32 | 33 | 34 | print compareString(A,B) -------------------------------------------------------------------------------- /anagram.py: -------------------------------------------------------------------------------- 1 | #-*-coding=utf-8-*- 2 | __author__ = 'xda' 3 | ''' 4 | wrong alghim: 5 | a='aac' 6 | b='acc' 7 | def anagram(s,t): 8 | for i in s: 9 | if i not in t: 10 | return False 11 | return True 12 | ''' 13 | a='abcc' 14 | b='cba' 15 | c='aaa' 16 | 17 | def anagram(s,t): 18 | s=list(s) 19 | t=list(t) 20 | print s 21 | print t 22 | #x= s.pop() 23 | if len(s)!=len(t): 24 | return False 25 | while s : 26 | 27 | x=s.pop() 28 | if x in t: 29 | t.remove(x) 30 | 31 | if len(s)==0 and len(t)==0: 32 | return True 33 | 34 | 35 | return False 36 | 37 | print anagram(a,b) --------------------------------------------------------------------------------