码迷,mamicode.com
首页 > 其他好文 > 详细

[lintcode][美国大公司][1.字符串处理]

时间:2015-09-04 11:08:17      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

两个字符串是变位词

技术分享
 1 class Solution:
 2     """
 3     @param s: The first string
 4     @param b: The second string
 5     @return true or false
 6     """
 7     def anagram(self, s, t):
 8         if len(s) == len(t):
 9             d = {}
10             for i in range(len(s)):  # full scan on each str
11                 if s[i] in d.keys():
12                     d[s[i]] += 1
13                 else:
14                     d[s[i]] = 1
15             for i in range(len(s)):
16                 if t[i] in d.keys():
17                     d[t[i]] -= 1
18                     if d[t[i]] == 0:
19                         d.pop(t[i])
20                 else:
21                     return False
22             return not d
23         return False
View Code

比较字符串

技术分享
 1 class Solution:
 2     """
 3     @param A : A string includes Upper Case letters
 4     @param B : A string includes Upper Case letters
 5     @return :  if string A contains all of the characters in B return True else return False
 6     """
 7     def compareStrings(self, A, B):
 8         if len(A) >= len(B):
 9             d = {}
10             for a in A:
11                 if a in d:
12                     d[a] += 1
13                 else:
14                     d[a] = 1
15             for b in B:
16                 if b in d:
17                     d[b] -= 1
18                     if d[b] == 0:
19                         d.pop(b)
20                 else:
21                     return False
22             return True
23         return False
View Code

字符串查找

技术分享
 1 class Solution:
 2     def strStr(self, source, target):
 3         if source != None and target != None:
 4             if source == target: return 0
 5             if not target: return 0
 6             len_s, len_t = len(source), len(target)
 7             if len_s >= len_t:
 8                 for i in xrange(len_s - len_t + 1):
 9                     if source[i] == target[0]:
10                         for j in xrange(1, len_t):
11                             if source[i+j] <> target[j]:
12                                 break
13                             if j == len_t - 1:
14                                 return i
15         return -1
View Code

乱序字符串

技术分享
 1 class Solution:
 2     def anagrams(self, strs):
 3         res = []
 4         if len(strs) >= 2:
 5             res_map = {}
 6             for s in strs:
 7                 count = [0] * 26  # all lower case chars
 8                 if s:
 9                     for c in s:  # count chars
10                         count[ord(c)-ord(a)] += 1
11                 tcount = tuple(count)  # tuple list as key (hash as key)
12                 if tcount in res_map:
13                     res_map[tuple(count)].append(s)
14                 else:
15                     ls = []
16                     ls.append(s)
17                     res_map[tuple(count)] = ls
18             for val in res_map.values():
19                 if len(val) > 1:  # anagrams
20                     for s in val:
21                         res.append(s)
22         return res
23 
24 
25 # class Solution:
26 #     # @param strs: A list of strings
27 #     # @return: A list of strings
28 #     def anagrams(self, strs):
29 #         res = []
30 #         if len(strs) >= 2:
31 #             cans = []
32 #             for s in strs:
33 #                 flag = False
34 #                 if cans:
35 #                     for c in cans:
36 #                         if self.is_anagrams(s, c):
37 #                             if c not in res: res.append(c)
38 #                             res.append(s)
39 #                             flag = True
40 #                             break
41 #                     if not flag: 
42 #                         cans.append(s)
43 #                 else:
44 #                     cans.append(s)
45 #         return res
46 
47         
48 #     def is_anagrams(self, s, t):
49 #         if len(s) == len(t):
50 #             d = {}
51 #             for i in range(len(s)):  # full scan on each str
52 #                 if s[i] in d.keys():
53 #                     d[s[i]] += 1
54 #                 else:
55 #                     d[s[i]] = 1
56 #             for i in range(len(s)):
57 #                 if t[i] in d.keys():
58 #                     d[t[i]] -= 1
59 #                     if d[t[i]] == 0:
60 #                         d.pop(t[i])
61 #                 else:
62 #                     return False
63 #             return not d
64 #         return False
View Code

 

[lintcode][美国大公司][1.字符串处理]

标签:

原文地址:http://www.cnblogs.com/t--c---/p/4781342.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!