-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLicenseKeyFormatting.java
More file actions
47 lines (41 loc) · 1.47 KB
/
LicenseKeyFormatting.java
File metadata and controls
47 lines (41 loc) · 1.47 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
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/*
* https://leetcode.com/problems/license-key-formatting/
*/
public class LicenseKeyFormatting {
public String licenseKeyFormatting(String s, int k) {
List<Character> characters = Arrays.stream(s.split("-"))
.flatMapToInt(String::chars)
.mapToObj(value -> (char) value)
.map(Character::toUpperCase)
.collect(Collectors.toList());
int extraCharacters = characters.size() % k;
String firstGroup = toString(characters.subList(0, extraCharacters));
String end = IntStream.range(0, characters.size() / k)
.mapToObj(i -> characters.subList(i * k + extraCharacters, (i + 1) * k + extraCharacters))
.map(this::toString)
.collect(Collectors.joining("-"));
if (end.isEmpty()) {
return firstGroup;
}
if (firstGroup.isEmpty()) {
return end;
}
return firstGroup + '-' + end;
}
private String toString(List<Character> characters) {
return characters
.stream()
.map(String::valueOf)
.collect(Collectors.joining());
}
public static void main(String[] args) {
System.out.println(new LicenseKeyFormatting().licenseKeyFormatting(
"2-5g-3-J",
2
)); // 2-5G-3J
}
}