-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstr.py
More file actions
159 lines (140 loc) · 4.63 KB
/
str.py
File metadata and controls
159 lines (140 loc) · 4.63 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
153
154
155
156
157
158
159
class Solution(object):
def compress(self, chars):
"""
:type chars: List[str]
:rtype: int
"""
# 100 pass
# Logic - Returning the count is enough, no need to pop the array and substitution + required count would work
# Overwrite the array as we traverse and donot pop or delete anything
# Reference from user javaleg
n = len(chars)
# as in example 2
if n <= 1:
return 1
# 2 pointer technique
init = 0
move = 1
total_count = 1
while move < n:
current_count = 1
# Count the number of similarities
while move < n and chars[move] == chars[init]:
move += 1
current_count += 1
# Count of 1 should be ignored
if current_count > 1:
# Count characters should be appened separately
current_count = str(current_count)
for ch in current_count:
init += 1
chars[init] = ch
total_count += 1
# The next character
init += 1
# Move is at the next diff char
if move < n:
chars[init] = chars[move]
total_count += 1
move += 1
return total_count
"""
i = 0
select = None
while i < len(chars):
if not select:
select = chars[i]
index = i
count = 1
i += 1
elif chars[i] == select:
count += 1
i += 1
elif chars[i] != select:
if count > 1:
chars = chars[:index+2]+list(str(count))+chars[i:]
select = None
i = len(chars[:index+2]+list(str(count)))
else:
select = None
if i >= len(chars) and count > 1:
chars = chars[:index+1]+list(str(count))
return len(chars)
"""
"""
# Compress string with ["Char", "no of times it occured"] - Fails some cases
# Use O(1) extra space ==> Dictionary method would have been easy, but use 0 space extra
i, count = 0,0
while i < len(chars):
count = 2
select = chars[i]
if chars[i+1] == select:
i += 2
chars[i] = 2
else:
i += 1
while chars[i] == select:
del chars[i]
count += 1
i += 1
return chars
"""
"""
# Fails large test case
i, count = 0,0
index = 0
ref = 0
while i < len(chars):
select = chars[i]
index = i
i += 1
count += 1
while i < len(chars) and chars[i] == select:
i += 1
count += 1
# Problems might occur here, anticipate and fix
if count > 1:
ref = list(str(count))
del chars[index+len(ref)+1:i]
chars[index] = select
for j in range(len(ref)):
index += 1
chars[index] = ref[j]
i = index#+len(ref)
ref = 0
count = 0
else:
count = 0
"""
"""
i = 0
count = 0
select = None
while i < len(chars):
#print str(i),select
if i < len(chars) and select is None:
print str(i),select, len(chars)
select = i
count = 1
i += 1
if i == len(chars):
return len(chars)
elif i < len(chars) and chars[i] == chars[select]:
count += 1
if not chars[select+1].isdigit():
chars[select+1] = str(count)
i += 1
elif count > 2:
chars[select+1] = str(count)
del chars[i]
else:
if count > 9:
temp = list(str(count))
chars = chars[:select+1]+temp+chars[select+len(temp):]
i = select+len(temp)+1
select = None
if i == len(chars):
return len(chars)
print str(i), chars
#return len(chars)
"""