-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interview Question (easy)
37 lines (23 loc) · 1.1 KB
/
Interview Question (easy)
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
7 kyu
Interview Question (easy)
You receive the name of a city as a string, and you need to return a string that shows how many times each letter shows up in the string by using asterisks (*).
For example:
"Chicago" --> "c:**,h:*,i:*,a:*,g:*,o:*"
As you can see, the letter c is shown only once, but with 2 asterisks.
The return string should include only the letters (not the dashes, spaces, apostrophes, etc). There should be no spaces in the output, and the different letters are separated by a comma (,) as seen in the example above.
Note that the return string must list the letters in order of their first appearance in the original string.
More examples:
"Bangkok" --> "b:*,a:*,n:*,g:*,k:**,o:*"
"Las Vegas" --> "l:*,a:**,s:**,v:*,e:*,g:*"
const getStrings = (city) => {
let arr = city.toLowerCase().split('').filter(e => e !== ' ');
let counter = {};
for(let c of arr) {
counter[c] = (counter[c] || 0) + 1;
}
let res = [];
for(let letter in counter) {
res.push(letter + ":" + '*'.repeat(counter[letter]));
}
return res.join(',');
}