Skip to content

Commit 2748c5e

Browse files
committed
feat: more exercises from chapter 7
1 parent 5aa5050 commit 2748c5e

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

chapter-7/ex3.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function findMissingLetter(str: string): string {
2+
let hashMap = new Map<string, boolean>()
3+
4+
for (const char of str) {
5+
if (hashMap.has(char)) {
6+
continue
7+
}
8+
hashMap.set(char, true)
9+
}
10+
11+
const alphabet: string = 'abcdefghijklmnopqrstuvwxyz'
12+
for (const letter of alphabet) {
13+
if (!hashMap.has(letter)) {
14+
return letter
15+
}
16+
}
17+
return 'No missing letter found'
18+
}

chapter-7/ex4.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function returnFirstNonDuplicated(str: string): string {
2+
const hashMap = new Map<string, boolean>()
3+
4+
for (const char of str) {
5+
if (hashMap.has(char)) {
6+
hashMap.set(char, false)
7+
} else {
8+
hashMap.set(char, true)
9+
}
10+
}
11+
12+
for (const [char, isUnique] of hashMap) {
13+
if (isUnique) {
14+
return char;
15+
}
16+
}
17+
return 'No non-duplicated character found'
18+
}

0 commit comments

Comments
 (0)