Skip to content

Commit 2495acb

Browse files
authored
Merge pull request ashutosh97#14 from zara-nicole/add-encoder
Encoder problem added
2 parents 29bcaa8 + 6cb851f commit 2495acb

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

EncoderProblem/Encoder.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.*;
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
5+
public class Encoder{
6+
7+
public static void main(String args[]){
8+
String[] arr1 = new String[]{"a"};
9+
String[] arr2 = new String[]{"1", "2", "3", "4"};
10+
String[] x = new String[arr1.length];
11+
x = encode(arr1,arr2);
12+
System.out.println(Arrays.toString(x));
13+
}
14+
15+
16+
public static String[] encode(String[] raw, String[] code_words) {
17+
18+
Map<String,String> map = new HashMap<String,String>();
19+
String[] x = new String[raw.length];
20+
for(int i=0; i<raw.length;i++) {
21+
if(map.containsKey(raw[i])){
22+
x[i] = map.get(raw[i]);
23+
break;
24+
} else {
25+
map.put(raw[i],code_words[i]);
26+
x[i] = map.get(raw[i]);
27+
}
28+
}
29+
return x;
30+
}
31+
32+
33+
}
34+

EncoderProblem/problem.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
Write a function that replaces the words in `raw` with the words in `code_words` such that the first occurrence of each word in `raw` is assigned the first unassigned word in `code_words`.
3+
4+
5+
encoder(["a"], ["1", "2", "3", "4"]) → ["1"]
6+
encoder(["a", "b"], ["1", "2", "3", "4"]) → ["1", "2"]
7+
encoder(["a", "b", "a"], ["1", "2", "3", "4"]) → ["1", "2", "1"]

0 commit comments

Comments
 (0)