-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathProblem1.java
More file actions
49 lines (41 loc) · 1.47 KB
/
Copy pathProblem1.java
File metadata and controls
49 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
48
49
package io.zipcoder;
import java.util.HashMap;
public class Problem1 {
private HashMap<Character, Character> replaceChars = new HashMap<Character, Character>();
public Problem1() {
replaceChars.put('f', '7');
replaceChars.put('s', '$');
replaceChars.put('1', '!');
replaceChars.put('a', '@');
}
public String replaceCharsByIteration(String input) {
char[] allChars = input.toCharArray();
for(int i = 0; i < allChars.length; i++) {
char c = allChars[i];
if(replaceChars.containsKey(Character.toLowerCase(c))) {
allChars[i] = replaceChars.get(Character.toLowerCase(c));
}
}
return new String(allChars);
}
public String replaceCharsByRecursion(String input) {
char[] allChars = input.toCharArray();
for(char c : replaceChars.keySet()) {
int index = input.indexOf(Character.toLowerCase(c));
if(index > -1) {
allChars[index] = replaceChars.get(c);
input = new String(allChars);
replaceCharsByRecursion(input);
}
else {
index = input.indexOf(Character.toUpperCase(c));
if(index > -1) {
allChars[index] = replaceChars.get(c);
input = new String(allChars);
replaceCharsByRecursion(input);
}
}
}
return input;
}
}