Skip to content

Commit 644c239

Browse files
Updates
2 parents 7f615a8 + dd40249 commit 644c239

File tree

13 files changed

+555
-2
lines changed

13 files changed

+555
-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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*Given two strings, write a method to decide
2+
if one is apermutation of the other permutation:*/
3+
4+
const isPermutation = (str1, str2) =>{
5+
if (str1.length !== str2.length) {
6+
return false;
7+
}
8+
const obj1 = determineCharCount(str1);
9+
const obj2 = determineCharCount(str2);
10+
for (let char of str1) {
11+
if (obj1[char] !== obj2[char]) {
12+
return false;
13+
}
14+
}
15+
return true;
16+
//******** helper function ******
17+
function determineCharCount(string) {
18+
const obj = {};
19+
for (let char of string) {
20+
if (obj[char] !== undefined) {
21+
obj[char]++;
22+
} else {
23+
obj[char] = 1;
24+
}
25+
}
26+
return obj;
27+
}
28+
}
29+
30+
// Tests:
31+
console.log(isPermutation('abc', 'abb') === false);
32+
console.log(isPermutation('abb', 'abc') === false);
33+
console.log(isPermutation('aaa', 'abc') === false);
34+
console.log(isPermutation('abc', 'abcd') === false);
35+
console.log(isPermutation('abc', 'bac') === true);
36+
console.log(isPermutation('', '') === true);
37+
console.log(isPermutation('12', '21') === true);
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+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Write a method to replace all spaces within a string with '%20'
2+
3+
const URLify = (string) => string.split(' ').join('%20');
4+
5+
// Tests:
6+
console.log(URLify('Hello World') === 'Hello%20World');
7+
console.log(URLify('') === '');
8+
console.log(URLify('This is an example') === 'This%20is%20an%20example');

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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Given a string, write a function to check if it is a permutation of a palindrome
2+
3+
const isPalindromePermutation = (str) => {
4+
const strNoSpaces = str.split(' ').join('');
5+
const obj = {};
6+
var oddCount = 0;
7+
for (let char of strNoSpaces) {
8+
if (obj[char] !== undefined) {
9+
obj[char]++;
10+
} else {
11+
obj[char] = 1;
12+
}
13+
}
14+
for (let key in obj) {
15+
if (obj[key] % 2) {
16+
oddCount++;
17+
}
18+
if (oddCount > 1) {
19+
return false;
20+
}
21+
}
22+
return true;
23+
}
24+
25+
console.log(isPalindromePermutation('tact coa') === true);
26+
console.log(isPalindromePermutation('tact cooa') === true);
27+
console.log(isPalindromePermutation('tacr coa') === false);
28+
console.log(isPalindromePermutation('tactr coa') === false);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* There are 3 types of edits that can be made on a string:
2+
insert character, remove character, replace a character
3+
Given 2 strings write a function that will check if it is
4+
1 or 0 edits away*/
5+
6+
const isOneAway = (str1, str2) => {
7+
if (str1.length === str2.length) {
8+
let errorCount = 0;
9+
for (let i = 0; i < str1.length; i++) {
10+
if (str1[i] !== str2[i]) {
11+
errorCount++;
12+
}
13+
if (errorCount > 1) {
14+
return false;
15+
}
16+
}
17+
} else {
18+
let errorCount = 0;
19+
const longestStr = findLongestStr(str1, str2);
20+
let x = 0;
21+
for(let i = 0; i < longestStr; i++) {
22+
if (str1[i] !== str2[x]) {
23+
errorCount++;
24+
x++;
25+
}
26+
if (errorCount > 1) {
27+
return false;
28+
}
29+
x++;
30+
}
31+
// ***** Helper functions ********
32+
function findLongestStr(str1, str2) {
33+
str1 > str2 ? str1 : str2;
34+
}
35+
}
36+
return true;
37+
}
38+
39+
// TESTS
40+
console.log(isOneAway('pale', 'ple') === true);
41+
console.log(isOneAway('pales', 'pale') === true);
42+
console.log(isOneAway('pale', 'bale') === true);
43+
console.log(isOneAway('pale', 'bake') === false);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Implement a method to perform basic string
2+
compression using the counts of repeated characters
3+
Ex: 'aabcccccaaa' would become a2b1c5a3
4+
*/
5+
6+
const stringCompression = (str) => {
7+
if (!str.length) {
8+
return '';
9+
}
10+
var compStr = '';
11+
var count = 1;
12+
var currentChar = str[0];
13+
for (let i = 1; i < str.length; i++) {
14+
let char = str[i];
15+
if (char === currentChar) {
16+
count++;
17+
if (i === str.length - 1) {
18+
compStr += `${currentChar}${count}`;
19+
}
20+
} else {
21+
compStr += `${currentChar}${count}`;
22+
currentChar = char;
23+
count = 1;
24+
}
25+
}
26+
return compStr;
27+
}
28+
29+
// TESTS
30+
console.log(stringCompression('aabcccccaaa') === 'a2b1c5a3');
31+
console.log(stringCompression('cccccccc') === 'c8');
32+
console.log(stringCompression('') === '');
33+
console.log(stringCompression('AabccCccaaa') === 'A1a1b1c2C1c2a3');
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)