Skip to content

Commit da2e863

Browse files
Ariana DavisAriana Davis
authored andcommitted
provided solutions for chapter01: 1.2 and 1.9 + chapter02: 2.2 in JavaScript
1 parent 7794e81 commit da2e863

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

JavaScript/chapter01/1.2 - Check Perm/solution.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,3 @@ var checkPerm = function(stringOne, stringTwo) {
1313
return sortStringOne === sortStringTwo;
1414
}
1515
};
16-
17-
//Tests
18-
console.log(checkPerm('aba' , 'aab'), true);

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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 a = new linkedList('1');
23+
var b = new linkedList('2');
24+
var c = new linkedList('3');
25+
var d = new linkedList('4');
26+
var e = new linkedList('5');
27+
var f = new linkedList('6');
28+
var g = new linkedList('7');
29+
30+
a.next = b;
31+
b.next = c;
32+
c.next = d;
33+
d.next = e;
34+
e.next = f;
35+
f.next = g;
36+
37+
findKthToLast(3, a);
38+
findKthToLast(10, a);
39+
findKthToLast(-1, a);
40+
findKthToLast(0, a);
41+
findKthToLast(1, a);

0 commit comments

Comments
 (0)