Skip to content

Commit 1151aa2

Browse files
committed
added working solution to One Away
1 parent fbd2a08 commit 1151aa2

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

chapter01/1.5 - OneAway/oneAway.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
var oneAway = function(string1, string2) {
2+
let obj = {}
3+
for (let i=0; i<string1.length; i++) {
4+
if (obj[string1[i]]) {
5+
obj[string1[i]]++
6+
} else {
7+
obj[string1[i]] = 1
8+
}
9+
}
10+
11+
for (let i=0; i<string2.length; i++) {
12+
if (obj[string2[i]]) {
13+
obj[string2[i]]--
14+
}
15+
}
16+
17+
let charCount = 0
18+
19+
for (let key in obj) {
20+
if (obj[key] !== 0) {
21+
charCount = charCount + obj[key]
22+
}
23+
}
24+
25+
if (charCount > 1) return false
26+
return true
227

3-
428
};
529

630

0 commit comments

Comments
 (0)