-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0482 License Key Formatting.py
43 lines (34 loc) · 1.24 KB
/
0482 License Key Formatting.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
41
42
43
# https://leetcode.com/problems/license-key-formatting/
# Version 1
class SolutionOne:
def licenseKeyFormatting(self, S: str, K: int) -> str:
part, parts = '', []
for i in range(len(S))[::-1]:
if S[i] == "-":
continue
part = f'{S[i].upper()}{part}'
if len(part) == K:
parts = [part] + parts
part = ''
if len(part) > 0:
parts = [part] + parts
# print(parts)
return '-'.join(parts)
# Version 2
class SolutionTwo:
def licenseKeyFormatting(self, S: str, K: int) -> str:
reverse = S.replace('-', '').upper()[::-1]
reverse_parts = [reverse[i:i + K][::-1] for i in range(0, len(reverse), K)]
return '-'.join(reverse_parts[::-1])
# Version 3
class SolutionThree:
def licenseKeyFormatting(self, S: str, K: int) -> str:
part, parts = '', []
for idx, val in enumerate(S.replace('-', '')[::-1]):
if len(part) == K:
parts.append(part)
part = ''
part = val.upper() + part
if part != '':
parts.append(part)
return '-'.join(parts[::-1])