Skip to content

Commit 74099be

Browse files
authored
Create decode-the-slanted-ciphertext.py
1 parent b243a4a commit 74099be

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+

0 commit comments

Comments
 (0)