-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path443.py
More file actions
152 lines (111 loc) · 3.69 KB
/
Copy path443.py
File metadata and controls
152 lines (111 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class Solution(object):
def compress(self, chars):
"""
:type chars: List[str]
:rtype: int
"""
if not chars:
return []
s = ""
currChar = chars[0]
currCharStart = 0
currCharEnd = 1
count = 0
delimiter = "$%"
## Iterate through the chars
## Track starting index
## Perform look-ahead for end index
## create the resulting array
## Get it up to length according to all the characters found with special character #
## remove all special characters
while count < len(chars)-1:
if chars[count] != chars[count+1]:
charLen = currCharEnd - currCharStart #2
temp = ""
if charLen > 1:
for i in str(charLen):
temp += i + delimiter
s += currChar + delimiter + temp # A2
## We need to replace the list with this
currCharStart = count + 1
currChar = chars[count+1]
currCharEnd += 1
count += 1
charLen = currCharEnd - currCharStart #2
if charLen > 1:
temp = ""
for i in str(charLen):
temp += i + delimiter
tempInpStr = currChar + delimiter + temp # A2
else:
tempInpStr = currChar
s += tempInpStr
if len(s) > 1:
chars[:] = [str(i) for i in s.split(delimiter)]
if chars[-1] == "":
chars[:] = chars[:-1]
else:
chars[:] = s.split(delimiter)
print(chars)
print(s)
return len(chars)
# class Solution(object):
# def compress(self, chars):
# n = len(chars)
# if n == 1:
# return 1
# write_idx = 0
# curr_idx = 0
# while curr_idx < n:
# curr_char = chars[curr_idx]
# cnt = 0
# while curr_idx < n and chars[curr_idx] == curr_char:
# curr_idx += 1
# cnt += 1
# chars[write_idx] = curr_char
# write_idx += 1
# if cnt > 1:
# cnt_str = str(cnt)
# for i in range(len(cnt_str)):
# chars[write_idx] = cnt_str[i]
# write_idx += 1
# return write_idx
# class Solution(object):
# def compress(self, chars):
# """
# :type chars: List[str]
# :rtype: int
# """
# if not chars:
# return []
# write_index = 0
# count = 0
# currPtr = 0
# delimiter = "$%"
# ## Iterate through the chars
# ## Track starting index
# ## Perform look-ahead for end index
# ## create the resulting array
# ## Get it up to length according to all the characters found with special character #
# ## remove all special characters
# while currPtr < len(chars):
# count = 0
# currChar = chars[currPtr]
# currCharStart = currPtr
# currCharEnd = currPtr
# while currPtr < len(chars) and chars[currPtr] == currChar:
# count += 1
# currPtr += 1
# currCharEnd += 1
# chars[currCharStart] = currChar
# write_index += 1
# if count > 1:
# count_str = str(count)
# for i in range(len(count_str)):
# chars[write_index] = count_str[i]
# write_index += 1
# return write_index
s = Solution()
chars = ["a","a","b","b","b", "c","c","c"]
print(s.compress(chars))
print(chars)