-
Notifications
You must be signed in to change notification settings - Fork 1
/
443.string-compression.py
41 lines (25 loc) · 991 Bytes
/
443.string-compression.py
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
#
# @lc app=leetcode id=443 lang=python3
#
# [443] String Compression
#
# @lc code=start
class Solution:
def compress(self, chars: List[str]) -> int:
"""Interesting algorithm; suprised this question is downvoted a lot;
Basically running a compression (basic basic) to merge consecutive characters
together; Similar techniques can be applied to many different solutions;
It is similar to a question I've screwed up in a Google interview before.
I misread the question and apprantly it involves doing an in-place transformation;
that is the reason why it is under voted.
#TODO: inplace fixing of the array
Args:
chars (List[str]): a string as a list
Returns:
int: the # of characters needed
"""
slen = len(chars)
# aa ab ba bb so a2 is still 2 and if not same it's still 2 :)
if slen < 3:
return slen
# @lc code=end