Skip to content

Commit f533992

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 3e8d7ce + 1b07d7e commit f533992

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

JavaScript/chapter01/1.1 - Is Unique/solution.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ Output: returns a deduped array of integers
44
*/
55

66
// Solution using Set
7-
87
const isUnique = (arr) => [...new Set(arr)];
98

109
// Test Cases
11-
1210
console.log(isUnique([1,1,1,2,2,2,2,3,3,3,3]) === [1,2,3]);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Given two strings, write a method to decide
2+
if one is permutation of the other */
3+
4+
var checkPerm = function(stringOne, stringTwo) {
5+
//if different lengths, return false
6+
if (stringOne.length !== stringTwo.length ) {
7+
return false;
8+
}
9+
//else sort & compare
10+
else {
11+
var sortStringOne = stringOne.split('').sort('').join('');
12+
var sortStringTwo = stringTwo.split('').sort('').join('');
13+
return sortStringOne === sortStringTwo;
14+
}
15+
};

JavaScript/chapter01/1.3 - URLify/solution.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ function URLify(arr, len) {
2323

2424
//testing
2525
let arr = ['M', 'r', ' ', 'J', 'o', 'h', 'n', ' ', 'S', 'm', 'i', 't', 'h', ' ', ' ', ' ', ' '];
26+
2627
//before
2728
console.log(arr);
2829
let ans = URLify(arr, 13);
30+
2931
//after
3032
console.log(ans);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* Assume you have a method isSubstring which checks if one word is a substring
2+
of another. Given two strings, sl and s2, write code to check if s2 is a rotation of sl using only one
3+
call to isSubstring (e.g., "waterbottle" is a rotation of"erbottlewat") */
4+
5+
var StringRotate = function(string1, string2) {
6+
if (string1.length !== string2.length ){
7+
return false;
8+
}
9+
return ( string2 + string1 ).includes(string1); // one call of Substring
10+
};
11+
12+
//Test
13+
console.log(StringRotate('waterbottle', 'erbottlewat'), true);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* Return Kth to Last: Implement an algorithm to find the kth to last element of a singly linked list. */
2+
3+
var linkedList = function(value) {
4+
this.value = value;
5+
this.next = null;
6+
};
7+
8+
var findKthToLast = function(k, head) {
9+
//do recursive
10+
if ( head == null || k < 1 ) {
11+
return;
12+
} else if ( k == 1 ) {
13+
console.log(head.value);
14+
findKthToLast(k, head.next)
15+
} else {
16+
findKthToLast( k-1, head.next);
17+
}
18+
};
19+
20+
/* Tests */
21+
var a = new linkedList('1');
22+
var b = new linkedList('2');
23+
var c = new linkedList('3');
24+
var d = new linkedList('4');
25+
var e = new linkedList('5');
26+
var f = new linkedList('6');
27+
var g = new linkedList('7');
28+
29+
a.next = b;
30+
b.next = c;
31+
c.next = d;
32+
d.next = e;
33+
e.next = f;
34+
f.next = g;
35+
36+
findKthToLast(3, a);
37+
findKthToLast(10, a);
38+
findKthToLast(-1, a);
39+
findKthToLast(0, a);
40+
findKthToLast(1, a);

0 commit comments

Comments
 (0)