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
+ # Time: O(n)
2
+ # Space: O(1)
3
+
4
+ class Solution (object ):
5
+ def decodeCiphertext (self , encodedText , rows ):
6
+ """
7
+ :type encodedText: str
8
+ :type rows: int
9
+ :rtype: str
10
+ """
11
+ cols = len (encodedText )// rows
12
+ k = len (encodedText )
13
+ for i in reversed (xrange (cols )):
14
+ for j in reversed (xrange (i , len (encodedText ), cols + 1 )):
15
+ if encodedText [j ] != ' ' :
16
+ k = j
17
+ break
18
+ else :
19
+ continue
20
+ break
21
+ result = []
22
+ for i in xrange (cols ):
23
+ for j in xrange (i , len (encodedText ), cols + 1 ):
24
+ result .append (encodedText [j ])
25
+ if j == k :
26
+ break
27
+ else :
28
+ continue
29
+ break
30
+ return "" .join (result )
31
+
32
+
33
+ # Time: O(n)
34
+ # Space: O(n)
35
+ class Solution2 (object ):
36
+ def decodeCiphertext (self , encodedText , rows ):
37
+ """
38
+ :type encodedText: str
39
+ :type rows: int
40
+ :rtype: str
41
+ """
42
+ cols = len (encodedText )// rows
43
+ result = []
44
+ for i in xrange (cols ):
45
+ for j in xrange (i , len (encodedText ), cols + 1 ):
46
+ result .append (encodedText [j ])
47
+ while result and result [- 1 ] == ' ' :
48
+ result .pop ()
49
+ return "" .join (result )
50
+
You can’t perform that action at this time.
0 commit comments