File tree 1 file changed +37
-0
lines changed
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Time: O(m * n), m is the number of words, n is the length of each word
2
+ # Space: O(1)
3
+
4
+ import collections
5
+
6
+
7
+ # freq table
8
+ class Solution (object ):
9
+ def oddString (self , words ):
10
+ """
11
+ :type words: List[str]
12
+ :rtype: str
13
+ """
14
+ for i in xrange (len (words [0 ])- 1 ):
15
+ lookup = collections .defaultdict (list )
16
+ for j , w in enumerate (words ):
17
+ if len (lookup [ord (w [i + 1 ])- ord (w [i ])]) < 2 :
18
+ lookup [ord (w [i + 1 ])- ord (w [i ])].append (j )
19
+ if len (lookup ) == 2 :
20
+ return next (words [l [0 ]] for l in lookup .itervalues () if len (l ) == 1 )
21
+
22
+
23
+ # Time: O(m * n), m is the number of words, n is the length of each word
24
+ # Space: O(n)
25
+ import collections
26
+
27
+
28
+ # freq table
29
+ class Solution2 (object ):
30
+ def oddString (self , words ):
31
+ """
32
+ :type words: List[str]
33
+ :rtype: str
34
+ """
35
+ cnt = collections .Counter (tuple (ord (w [i + 1 ])- ord (w [i ]) for i in xrange (len (w )- 1 )) for w in words )
36
+ target = next (k for k , v in cnt .iteritems () if v == 1 )
37
+ return next (w for w in words if tuple (ord (w [i + 1 ])- ord (w [i ]) for i in xrange (len (w )- 1 )) == target )
You can’t perform that action at this time.
0 commit comments