Skip to content

Commit d32bf5a

Browse files
add: frequency of alphabet letters
1 parent a775717 commit d32bf5a

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- [Finding all the anagrams](finding-all-the-anagrams)
55
- [Finding the maximum depth of a binary tree](finding-the-maximum-depth-of-a-binary-tree)
66
- [Fixing setting up a callback](fixing-setting-up-a-callback)
7+
- [Frequency of alphabet letters](frequency-of-alphabet-letters)
78
- [Getting the distinct transactions](getting-the-distinct-transactions)
89
- [Replacing characters](replacing-characters)
910
- [Replacing with the cipher letters](replacing-with-the-cipher-letters)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Frequency of alphabet letters
2+
3+
In this problem we are asking for implementation in JavaScript of a function, g, that returns the frequency of alphabet letters in a given string ignoring upper/lower case issues.
4+
5+
Use whatever programming language you are most comfortable with, although we prefer to see standard scripting languages used.
6+
7+
The return format is best demonstrated in an example. Suppose you are given the String “Hello there! Apple!" The function g shall return the following structure:
8+
9+
```console
10+
{
11+
a:1,
12+
b:0,
13+
c:0,
14+
d:0,
15+
e:4,
16+
f:0,
17+
g:0,
18+
h:2,
19+
i:0,
20+
j:0,
21+
k:0,
22+
l:3,
23+
m:0,
24+
n:0,
25+
o:1,
26+
p:2,
27+
q:0,
28+
r:1,
29+
s:0,
30+
t:1,
31+
u:0,
32+
v:0,
33+
w:0,
34+
x:0,
35+
y:0,
36+
z:0
37+
}
38+
```
39+
40+
## Execute
41+
42+
```bash
43+
node solution.js
44+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function g(str) {
2+
const letterFrequencies = {};
3+
4+
let codeA = 'a'.charCodeAt(0);
5+
const codeZ = 'z'.charCodeAt(0);
6+
7+
while (codeA <= codeZ) {
8+
const letter = String.fromCharCode(codeA++);
9+
10+
letterFrequencies[letter] = str.replace(
11+
new RegExp(`[^${letter}]`, 'gi'),
12+
'',
13+
).length;
14+
}
15+
16+
return letterFrequencies;
17+
}
18+
19+
(() => {
20+
console.log(g('Hello there! Apple!'));
21+
})();

0 commit comments

Comments
 (0)