File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed
Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ # coding: utf-8
2+
3+ class Solution :
4+ """
5+ @param: words: A list of words
6+ @return: Return how many different rotate words
7+ """
8+ def countRotateWords (self , words ):
9+ # Write your code here
10+ result = {}
11+ for word in words :
12+ tp_word = tuple (word )
13+ if tp_word not in result :
14+ result [tp_word ] = 1
15+ # 在这里就直接展开方便后面查表
16+ # 展开后的如果没出现过就置0,
17+ # 因为我们只统计多少种不同的类型。
18+ for i in xrange (1 , len (word )):
19+ shifed_word = Solution ._shift (list (word ), i )
20+ if shifed_word not in result :
21+ result [shifed_word ] = 0
22+ ret = 0
23+ for k , v in result .items ():
24+ ret += v
25+ return ret
26+
27+ @staticmethod
28+ def _match (target , word ):
29+ li_word = list (word )
30+ for i in xrange (len (word )):
31+ if Solution ._shift (li_word , i ) == target :
32+ return True
33+ return False
34+
35+ @staticmethod
36+ def _reverse (word , start , end ):
37+ while start < end :
38+ word [start ], word [end ] = word [end ], word [start ]
39+ start += 1
40+ end -= 1
41+
42+ @staticmethod
43+ def _shift (word , n ):
44+ len_ = len (word )
45+ Solution ._reverse (word , 0 , len_ - n - 1 )
46+ Solution ._reverse (word , len_ - n , len_ - 1 )
47+ Solution ._reverse (word , 0 , len_ - 1 )
48+ return tuple (word )
49+
50+ # esay: http://lintcode.com/zh-cn/problem/rotate-words/
You can’t perform that action at this time.
0 commit comments