-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrongPassword.java
More file actions
120 lines (108 loc) · 3.9 KB
/
StrongPassword.java
File metadata and controls
120 lines (108 loc) · 3.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package Divisions.LevelUnrated;
//code by senurah
import java.util.HashSet;
import java.util.Scanner;
public class StrongPassword {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
scanner.nextLine();
int replace = -1;
HashSet<Character> charSet = new HashSet<>();
if (t >= 1 && t <= 1000) {
for (int i = 0; i < t; i++) {
String line = scanner.nextLine();
String [] password = line.split("");
char [] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
HashSet<Character> alphabetSet = new HashSet<>();
for (char c = 'a'; c <= 'z'; c++) {
alphabetSet.add(c);
}
///finding unique char
for (char c : line.toCharArray()) {
alphabetSet.remove(c);
}
char uniqeChar = returnUnique4(charSet);
if(password.length==1){
replace = 0;
}
for(int j = 0; j< password.length-1;j++){
if((password[j+1] != null && (password[j].equals(password[j+1])))){
replace = j+1;
break;
}else{
//all unique
replace = -2;
}
}
//rebuilding
if(replace==0){
System.out.print(uniqeChar);
for(String s: password){
System.out.print(s);
}
System.out.println();
}else if(replace== -2){
System.out.print(uniqeChar);
for(String a :password){
System.out.print(a);
}
System.out.println();
}else{
for(int k = 0; k <replace;k++){
System.out.print(password[k]);
}
System.out.print(uniqeChar);
for(int n =replace;n< password.length;n++){
System.out.print(password[n]);
}
System.out.println();
}
}
}
}
public static char returnUnique(HashSet<Character> charSet){
for(char c = 'b'; c<= 'z';c++){
if(!charSet.contains(c)){
return c;
}
}
return 'x';
}
public static char returnUnique2(HashSet<Character> charSet,char[] alphabet){
HashSet<Character> uniqueChars = new HashSet<>();
for(char j : alphabet){
if(!charSet.contains(j)){
uniqueChars.add(j);
}
}
Character[] tempArray = uniqueChars.toArray(new Character[0]);
char [] charArray = new char[tempArray.length];
for (int i = 0; i < tempArray.length; i++) {
charArray[i] = tempArray[i];
}
//uniqueChars.toArray(charArray);
return charArray[0];
}
public static char returnUnique3(HashSet<Character> alphabetSet){
char[] missingLetters = new char[alphabetSet.size()];
int index = 0;
for (char c : alphabetSet) {
missingLetters[index++] = c;
}
return missingLetters[0];
}
public static char returnUnique4(HashSet<Character> alphabetSet) {
// Check if alphabetSet is empty
if (alphabetSet.isEmpty()) {
return 'x'; // or any default character
}
// Convert HashSet to an array and return the first element
char[] missingLetters = new char[alphabetSet.size()];
int index = 0;
for (char c : alphabetSet) {
missingLetters[index++] = c;
}
return missingLetters[0];
}
}