Skip to content

Commit de29f3b

Browse files
authored
Merge pull request #11 from rroque98/master
Add chapter 1 JavaScript solutions to 1.2 - 1.6
2 parents 1b07d7e + f533992 commit de29f3b

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed
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: 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');
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');

0 commit comments

Comments
 (0)