-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotKeys.java
More file actions
59 lines (50 loc) · 1.58 KB
/
HotKeys.java
File metadata and controls
59 lines (50 loc) · 1.58 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
package Recursion;
import javax.lang.model.util.Types;
import java.util.*;
public class HotKeys {
/**
* Given a list of strings, assign a unique character as a hotkey.
* 1. Hotkey of a string should be a character of the string
* 2. All hotkeys must be unique
*
* Ex['front', 'back', 'left', 'right', 'fly']
* Output [f, b, l, r, y]
*
* ["forward", "backward", "left", "right", "fly", "flye", "flyee"]
* [f,b,t,l,y,e]
*/
public static Map<Character, String> hotKeys(List<String> input){
Map<Character, String> result = new HashMap<>();
helper(input, 0, result);
return result;
}
private static void helper(List<String> input, int index,
Map<Character, String> result ) {
if (index == input.size()) {
return;
}
else {
String s = input.get(index);
for (int i =0; i < s.length(); i++){
char c = s.charAt(i);
if (! result.containsKey(c)){
result.put(c, s);
helper(input, index+1, result);
if(result.size() == input.size())
break;
result.remove(c);
}
}
}
}
public static void main(String args[]){
List<String> input = new ArrayList<>();
input.add("forward");
input.add("left");
input.add("fly");
input.add("flye");
input.add("flyee");
input.add("flyeee");
System.out.println(hotKeys(input));
}
}