-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduplicateEncoder.js
51 lines (42 loc) · 1.14 KB
/
duplicateEncoder.js
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
function duplicateEncode(word){
var input = (word.toLowerCase()).split('');
var output ="";
//make object from input string
createObj = (inputArr)=> {
let x;
var result ={};
for(x in inputArr){
// console.log(inputArr[x]);
if(!result[inputArr[x]]){
result[inputArr[x]] = 1;
}
else{
result[inputArr[x]] +=1;
}
}
return result;
}
//take in object output string
createNewString = (arr,inputObj) =>{
let y;
let output="";
console.log(inputObj);
console.log(arr);
for(y in arr){
console.log(inputObj[arr[y]]);
if(inputObj[arr[y]]==1){
output += "(";
}
else{
output+=")";
}
}
return output;
}
createObj(input);
console.log(createNewString(input,createObj(input)));
}
// ()()()
// "(" if that character appears only once in the original string,
// or ")" if that character appears more than once
duplicateEncode('Success');