File tree 2 files changed +35
-20
lines changed
2 files changed +35
-20
lines changed Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def findLength (self , A , B ):
3
+ """
4
+ :type A: List[int]
5
+ :type B: List[int]
6
+ :rtype: int
7
+ """
8
+ # dp[i][j] represents A[:i + 1], B[:j + 1] longest common subarray length
9
+ dp = [[0 for _ in range (len (B ) + 1 )] for _ in range (len (A ) + 1 )]
10
+
11
+ res = 0
12
+
13
+ for i in range (1 , len (A ) + 1 ):
14
+ for j in range (1 , len (B ) + 1 ):
15
+ if A [i - 1 ] == B [j - 1 ]:
16
+ dp [i ][j ] = max (dp [i ][j ], dp [i - 1 ][j - 1 ] + 1 )
17
+ res = max (dp [i ][j ], res )
18
+ # print dp
19
+ return res
Original file line number Diff line number Diff line change @@ -4,23 +4,19 @@ def subdomainVisits(self, cpdomains):
4
4
:type cpdomains: List[str]
5
5
:rtype: List[str]
6
6
"""
7
- resList = []
8
- resMap = {}
9
- for s in cpdomains :
10
- count , domains = s .split (' ' )
11
- n = domains .count ('.' )
12
- tmp = domains
13
- for i in range (n + 1 ):
14
- if resMap .has_key (tmp ):
15
- resMap [tmp ] = resMap [tmp ] + int (count )
16
- else :
17
- resMap [tmp ] = int (count )
18
- index = tmp .find ('.' ) + 1
19
- if index == - 1 :
20
- break
21
- else :
22
- tmp = tmp [index :]
23
- # for key, value in resMap.items():
24
- # resList.append(str(value) + ' ' + key);
25
- # return resList
26
- return [str (resMap [key ]) + ' ' + key for key in resMap ]
7
+ from collections import defaultdict
8
+ dic = defaultdict (int )
9
+
10
+ for pair in cpdomains :
11
+ splitted_pair = pair .split ()
12
+ cnt , domain = splitted_pair [0 ], splitted_pair [1 ]
13
+ cnt = int (cnt )
14
+
15
+ for i in range (len (domain )):
16
+ if not i or domain [i ] == "." :
17
+ dic [domain [i :].lstrip ("." )] += cnt
18
+
19
+ res = []
20
+ for domain , frequency in dic .items ():
21
+ res .append (" " .join ([str (frequency ), domain ]))
22
+ return res
You can’t perform that action at this time.
0 commit comments