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

两个字符串是变位词

 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

乱序字符串

First idea is brute force, keep a candidates list to store the tragets strs, then compare one by one (this compare would generate hashmap for both strs then do the compare), see solution been commented and I failed at last case for time limitation. I think over it and realized that I do not need generate mapping each time, similiar thinking like candidates list, we can keep a candidates hash mapping, so only need generate each strs hashing and compare with target mappings.

The bonus point for Python is tuple can be key as it can be hashed, so I used tuple(list) as key for the hash mapping, and I store the strs into result list while I do the checking, saved some space and traverse time. (compare the two solutions below)

Let's look back to the difference, it is typical Time or Space solution, apparently TIME goes first.

 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
class Solution:
    def anagrams(self, strs):
        res = []
        if len(strs) >= 2:
            res_map = {}
            for s in strs:
                count = [0] * 26  # all lower case chars
                if s:  # avoid ""
                    for c in s:  # count chars
                        count[ord(c)-ord('a')] += 1
                tcount = tuple(count)  # tuple list as key (hash as key)
                if tcount in res_map:
                    if not res_map[tcount] in res:
                        res.append(res_map[tcount])
                    res.append(s)
                else:
                    res_map[tcount] = s
        return res

最长公共子串

http://segmentfault.com/a/1190000002641054

 

posted @ 2015-09-04 10:37  tanchao  阅读(233)  评论(0编辑  收藏  举报