Skip to content

Commit 01db6d9

Browse files
committedOct 19, 2018
more movings
1 parent e0a502b commit 01db6d9

File tree

126 files changed

+6662
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+6662
-6
lines changed
 
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
1.2 Check Permutation: Given two strings,
3+
write a method to decide if one is a permutation of the other.
4+
*/
5+
function checkPermutation(a, b){
6+
if(a.length !== b.length) return false;
7+
8+
chars = new Map();
9+
10+
a.split('').forEach((c) => chars.set(c, 1));
11+
12+
for(const c of b){
13+
if(chars.get(c) !== 1) return false;
14+
}
15+
return true;
16+
}
17+
18+
console.log(checkPermutation('123', '231'));
19+
console.log(checkPermutation('123', '456'));
20+
console.log(checkPermutation('123', '4231'));
21+
console.log(checkPermutation('4231', '123'));
22+
console.log(checkPermutation('God', 'dog'));
23+
console.log(checkPermutation('God ', 'dog'));

‎lab/exercises/01-arrays/one-away.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function isOneEditAway(str1, str2) {
2+
let edit = 0;
3+
let i1, i2;
4+
5+
if(Math.abs(str1.length - str2.length) > 1) {
6+
return false;
7+
}
8+
9+
for(i1 = 0, i2 = 0; i1 < str1.length && i2 < str2.length; i1++, i2++) {
10+
if(str1[i1] === str2[i2]) {
11+
continue;
12+
} else {
13+
edit++;
14+
15+
if(edit > 1){
16+
return false;
17+
} else if(str1.length > str2.length) {
18+
i2--;
19+
} else if(str1.length < str2.length) {
20+
i1--;
21+
} else {
22+
continue;
23+
}
24+
}
25+
}
26+
27+
return edit <= 1;
28+
}
29+
30+
console.log(isOneEditAway('pale', 'pale')); // true
31+
console.log(isOneEditAway('pale', 'ale')); // true
32+
console.log(isOneEditAway('pale', 'pales')); // true
33+
console.log(isOneEditAway('pale', 'bale')); // true
34+
console.log(isOneEditAway('apple', 'aple')); // true
35+
36+
console.log(isOneEditAway('pale', 'bae')); // false
37+
console.log(isOneEditAway('pale', 'elap')); // false
38+
console.log(isOneEditAway('pale', 'palepalepale')); // false

0 commit comments

Comments
 (0)
Please sign in to comment.