Skip to content

Commit 5389785

Browse files
committed
feat: add solutions to lc problem: No.0482.License Key Formatting
1 parent 19aee62 commit 5389785

File tree

6 files changed

+281
-2
lines changed

6 files changed

+281
-2
lines changed

solution/0400-0499/0482.License Key Formatting/README.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,119 @@
4646

4747
<!-- 这里可写通用的实现逻辑 -->
4848

49+
简单模拟。
50+
4951
<!-- tabs:start -->
5052

5153
### **Python3**
5254

5355
<!-- 这里可写当前语言的特殊实现逻辑 -->
5456

5557
```python
56-
58+
class Solution:
59+
def licenseKeyFormatting(self, s: str, k: int) -> str:
60+
s = s.replace('-', '').upper()
61+
res = []
62+
cnt = (len(s) % k) or k
63+
t = 0
64+
for i, c in enumerate(s):
65+
res.append(c)
66+
t += 1
67+
if t == cnt:
68+
t = 0
69+
cnt = k
70+
if i != len(s) - 1:
71+
res.append('-')
72+
return ''.join(res)
5773
```
5874

5975
### **Java**
6076

6177
<!-- 这里可写当前语言的特殊实现逻辑 -->
6278

6379
```java
80+
class Solution {
81+
public String licenseKeyFormatting(String s, int k) {
82+
s = s.replace("-", "").toUpperCase();
83+
StringBuilder sb = new StringBuilder();
84+
int t = 0;
85+
int cnt = s.length() % k;
86+
if (cnt == 0) {
87+
cnt = k;
88+
}
89+
for (int i = 0; i < s.length(); ++i) {
90+
sb.append(s.charAt(i));
91+
++t;
92+
if (t == cnt) {
93+
t = 0;
94+
cnt = k;
95+
if (i != s.length() - 1) {
96+
sb.append('-');
97+
}
98+
}
99+
}
100+
return sb.toString();
101+
}
102+
}
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
string licenseKeyFormatting(string s, int k) {
111+
string ss = "";
112+
for (char c : s)
113+
{
114+
if (c == '-') continue;
115+
if ('a' <= c && c <= 'z') c += 'A' - 'a';
116+
ss += c;
117+
}
118+
int cnt = ss.size() % k;
119+
if (cnt == 0) cnt = k;
120+
int t = 0;
121+
string res = "";
122+
for (int i = 0; i < ss.size(); ++i)
123+
{
124+
res += ss[i];
125+
++t;
126+
if (t == cnt)
127+
{
128+
t = 0;
129+
cnt = k;
130+
if (i != ss.size() - 1) res += '-';
131+
}
132+
}
133+
return res;
134+
}
135+
};
136+
```
64137
138+
### **Go**
139+
140+
```go
141+
func licenseKeyFormatting(s string, k int) string {
142+
s = strings.ReplaceAll(s, "-", "")
143+
cnt := len(s) % k
144+
if cnt == 0 {
145+
cnt = k
146+
}
147+
t := 0
148+
res := []byte{}
149+
for i, c := range s {
150+
res = append(res, byte(unicode.ToUpper(c)))
151+
t++
152+
if t == cnt {
153+
t = 0
154+
cnt = k
155+
if i != len(s)-1 {
156+
res = append(res, byte('-'))
157+
}
158+
}
159+
}
160+
return string(res)
161+
}
65162
```
66163

67164
### **...**

solution/0400-0499/0482.License Key Formatting/README_EN.md

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,108 @@ Note that the two extra dashes are not needed and can be removed.
4545
### **Python3**
4646

4747
```python
48-
48+
class Solution:
49+
def licenseKeyFormatting(self, s: str, k: int) -> str:
50+
s = s.replace('-', '').upper()
51+
res = []
52+
cnt = (len(s) % k) or k
53+
t = 0
54+
for i, c in enumerate(s):
55+
res.append(c)
56+
t += 1
57+
if t == cnt:
58+
t = 0
59+
cnt = k
60+
if i != len(s) - 1:
61+
res.append('-')
62+
return ''.join(res)
4963
```
5064

5165
### **Java**
5266

5367
```java
68+
class Solution {
69+
public String licenseKeyFormatting(String s, int k) {
70+
s = s.replace("-", "").toUpperCase();
71+
StringBuilder sb = new StringBuilder();
72+
int t = 0;
73+
int cnt = s.length() % k;
74+
if (cnt == 0) {
75+
cnt = k;
76+
}
77+
for (int i = 0; i < s.length(); ++i) {
78+
sb.append(s.charAt(i));
79+
++t;
80+
if (t == cnt) {
81+
t = 0;
82+
cnt = k;
83+
if (i != s.length() - 1) {
84+
sb.append('-');
85+
}
86+
}
87+
}
88+
return sb.toString();
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
string licenseKeyFormatting(string s, int k) {
99+
string ss = "";
100+
for (char c : s)
101+
{
102+
if (c == '-') continue;
103+
if ('a' <= c && c <= 'z') c += 'A' - 'a';
104+
ss += c;
105+
}
106+
int cnt = ss.size() % k;
107+
if (cnt == 0) cnt = k;
108+
int t = 0;
109+
string res = "";
110+
for (int i = 0; i < ss.size(); ++i)
111+
{
112+
res += ss[i];
113+
++t;
114+
if (t == cnt)
115+
{
116+
t = 0;
117+
cnt = k;
118+
if (i != ss.size() - 1) res += '-';
119+
}
120+
}
121+
return res;
122+
}
123+
};
124+
```
54125
126+
### **Go**
127+
128+
```go
129+
func licenseKeyFormatting(s string, k int) string {
130+
s = strings.ReplaceAll(s, "-", "")
131+
cnt := len(s) % k
132+
if cnt == 0 {
133+
cnt = k
134+
}
135+
t := 0
136+
res := []byte{}
137+
for i, c := range s {
138+
res = append(res, byte(unicode.ToUpper(c)))
139+
t++
140+
if t == cnt {
141+
t = 0
142+
cnt = k
143+
if i != len(s)-1 {
144+
res = append(res, byte('-'))
145+
}
146+
}
147+
}
148+
return string(res)
149+
}
55150
```
56151

57152
### **...**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
string licenseKeyFormatting(string s, int k) {
4+
string ss = "";
5+
for (char c : s)
6+
{
7+
if (c == '-') continue;
8+
if ('a' <= c && c <= 'z') c += 'A' - 'a';
9+
ss += c;
10+
}
11+
int cnt = ss.size() % k;
12+
if (cnt == 0) cnt = k;
13+
int t = 0;
14+
string res = "";
15+
for (int i = 0; i < ss.size(); ++i)
16+
{
17+
res += ss[i];
18+
++t;
19+
if (t == cnt)
20+
{
21+
t = 0;
22+
cnt = k;
23+
if (i != ss.size() - 1) res += '-';
24+
}
25+
}
26+
return res;
27+
}
28+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func licenseKeyFormatting(s string, k int) string {
2+
s = strings.ReplaceAll(s, "-", "")
3+
cnt := len(s) % k
4+
if cnt == 0 {
5+
cnt = k
6+
}
7+
t := 0
8+
res := []byte{}
9+
for i, c := range s {
10+
res = append(res, byte(unicode.ToUpper(c)))
11+
t++
12+
if t == cnt {
13+
t = 0
14+
cnt = k
15+
if i != len(s)-1 {
16+
res = append(res, byte('-'))
17+
}
18+
}
19+
}
20+
return string(res)
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public String licenseKeyFormatting(String s, int k) {
3+
s = s.replace("-", "").toUpperCase();
4+
StringBuilder sb = new StringBuilder();
5+
int t = 0;
6+
int cnt = s.length() % k;
7+
if (cnt == 0) {
8+
cnt = k;
9+
}
10+
for (int i = 0; i < s.length(); ++i) {
11+
sb.append(s.charAt(i));
12+
++t;
13+
if (t == cnt) {
14+
t = 0;
15+
cnt = k;
16+
if (i != s.length() - 1) {
17+
sb.append('-');
18+
}
19+
}
20+
}
21+
return sb.toString();
22+
}
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def licenseKeyFormatting(self, s: str, k: int) -> str:
3+
s = s.replace('-', '').upper()
4+
res = []
5+
cnt = (len(s) % k) or k
6+
t = 0
7+
for i, c in enumerate(s):
8+
res.append(c)
9+
t += 1
10+
if t == cnt:
11+
t = 0
12+
cnt = k
13+
if i != len(s) - 1:
14+
res.append('-')
15+
return ''.join(res)

0 commit comments

Comments
 (0)