-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem1_UsernameAvailabilityChecker.java
More file actions
69 lines (52 loc) · 1.9 KB
/
Problem1_UsernameAvailabilityChecker.java
File metadata and controls
69 lines (52 loc) · 1.9 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.*;
class UsernameChecker {
private HashMap<String, Integer> users = new HashMap<>();
private HashMap<String, Integer> attempts = new HashMap<>();
public UsernameChecker() {
users.put("john_doe", 1);
users.put("admin", 2);
users.put("alex99", 3);
}
public boolean checkAvailability(String username) {
attempts.put(username, attempts.getOrDefault(username, 0) + 1);
return !users.containsKey(username);
}
public List<String> suggestAlternatives(String username) {
List<String> suggestions = new ArrayList<>();
if (!users.containsKey(username)) {
suggestions.add(username);
return suggestions;
}
for (int i = 1; i <= 5; i++) {
String suggestion = username + i;
if (!users.containsKey(suggestion)) {
suggestions.add(suggestion);
}
}
String dotVersion = username.replace("_", ".");
if (!users.containsKey(dotVersion)) {
suggestions.add(dotVersion);
}
return suggestions;
}
public String getMostAttempted() {
String mostAttempted = "";
int max = 0;
for (Map.Entry<String, Integer> entry : attempts.entrySet()) {
if (entry.getValue() > max) {
max = entry.getValue();
mostAttempted = entry.getKey();
}
}
return mostAttempted + " (" + max + " attempts)";
}
}
public class Problem1_UsernameAvailabilityChecker {
public static void main(String[] args) {
UsernameChecker checker = new UsernameChecker();
System.out.println(checker.checkAvailability("john_doe"));
System.out.println(checker.checkAvailability("jane_smith"));
System.out.println(checker.suggestAlternatives("john_doe"));
System.out.println(checker.getMostAttempted());
}
}