Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added: string compression
  • Loading branch information
mihirs16 committed Mar 2, 2023
commit d6df4b013d9b980759fbaa692313c31350da0d37
22 changes: 22 additions & 0 deletions 14. Questions/leetcode 443 - string compression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# string compression | leetcode 443 | https://leetcode.com/problems/string-compression/
# sliding window to keep track of a char's occurence

class Solution:
def compress(self, chars: list[str]) -> int:
ptrL, ptrR = 0, 0
total = 0
chars += " "

while ptrR < len(chars):
if chars[ptrL] != chars[ptrR]:
chars[total] = chars[ptrL]
total += 1
group = ptrR - ptrL
if group > 1:
for x in str(group):
chars[total] = x
total += 1
ptrL = ptrR
ptrR += 1

return total