Skip to content

Commit c6538cb

Browse files
authored
Merge pull request #26 from rroque98/refactorSolutionsChap1
Refactor JavaScript Solutions to 1.2, 1.3, 1.4, 1.6
2 parents 475b8e8 + eead34c commit c6538cb

File tree

4 files changed

+45
-46
lines changed

4 files changed

+45
-46
lines changed
Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
11
/*Given two strings, write a method to decide
22
if one is apermutation of the other permutation:*/
33

4-
const isPermutation = (str1, str2) =>{
4+
const isPermutation = (str1, str2) => {
55
if (str1.length !== str2.length) {
66
return false;
77
}
8-
const obj1 = determineCharCount(str1);
9-
const obj2 = determineCharCount(str2);
10-
for (let char of str1) {
11-
if (obj1[char] !== obj2[char]) {
8+
const freq1 = determineCharCount(str1);
9+
const freq2 = determineCharCount(str2);
10+
for (const [char, count] of freq1) {
11+
if (count !== freq2.get(char)) {
1212
return false;
1313
}
1414
}
1515
return true;
1616
//******** helper function ******
1717
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;
18+
return string
19+
.split('')
20+
.reduce(
21+
(acc, char) => acc.set(char, (acc.get(char) || 0) + 1),
22+
new Map()
23+
);
2724
}
28-
}
25+
};
2926

3027
// 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);
28+
console.log(isPermutation('abc', 'abb') === false);
29+
console.log(isPermutation('abb', 'abc') === false);
30+
console.log(isPermutation('aaa', 'abc') === false);
31+
console.log(isPermutation('abc', 'abcd') === false);
32+
console.log(isPermutation('abc', 'bac') === true);
33+
console.log(isPermutation('', '') === true);
34+
console.log(isPermutation('12', '21') === true);
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Write a method to replace all spaces within a string with '%20'
22

3-
const URLify = (string) => string.split(' ').join('%20');
3+
const encodeSpaces = (string) => string.replace(/ /g, '%20');
44

55
// 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');
6+
console.log(encodeSpaces('Hello World') === 'Hello%20World');
7+
console.log(encodeSpaces('') === '');
8+
console.log(encodeSpaces('This is an example') === 'This%20is%20an%20example');

JavaScript/chapter01/1.4 - PalinPerm/rroque98_sol.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
// Given a string, write a function to check if it is a permutation of a palindrome
22

33
const isPalindromePermutation = (str) => {
4-
const strNoSpaces = str.split(' ').join('');
5-
const obj = {};
4+
const strNoSpaces = str.replace(/ /g, '');
65
var oddCount = 0;
6+
const frequencies = new Map();
77
for (let char of strNoSpaces) {
8-
if (obj[char] !== undefined) {
9-
obj[char]++;
10-
} else {
11-
obj[char] = 1;
12-
}
8+
frequencies.set(char, 1 + (frequencies.get(char) || 0));
139
}
14-
for (let key in obj) {
15-
if (obj[key] % 2) {
16-
oddCount++;
10+
for (let frequency of frequencies.values()) {
11+
if (frequency % 2 === 0) {
12+
continue;
1713
}
14+
oddCount++;
1815
if (oddCount > 1) {
1916
return false;
2017
}
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
1-
/* Implement a method to perform basic string
2-
compression using the counts of repeated characters
1+
/* Implement a method to perform basic string
2+
compression using the counts of repeated characters.
3+
If the compressed string length is more than original
4+
string length, return original string.
35
Ex: 'aabcccccaaa' would become a2b1c5a3
46
*/
57

68
const stringCompression = (str) => {
79
if (!str.length) {
810
return '';
911
}
10-
var compStr = '';
11-
var count = 1;
12-
var currentChar = str[0];
13-
for (let i = 1; i < str.length; i++) {
12+
let compStr = '';
13+
let count = 0;
14+
let currentChar = str[0];
15+
for (let i = 0; i < str.length; i++) {
1416
let char = str[i];
1517
if (char === currentChar) {
1618
count++;
17-
if (i === str.length - 1) {
18-
compStr += `${currentChar}${count}`;
19-
}
2019
} else {
2120
compStr += `${currentChar}${count}`;
2221
currentChar = char;
2322
count = 1;
2423
}
2524
}
25+
compStr += `${currentChar}${count}`;
26+
if (compStr.length > str.length) {
27+
return str;
28+
}
2629
return compStr;
2730
}
2831

2932
// TESTS
3033
console.log(stringCompression('aabcccccaaa') === 'a2b1c5a3');
3134
console.log(stringCompression('cccccccc') === 'c8');
3235
console.log(stringCompression('') === '');
33-
console.log(stringCompression('AabccCccaaa') === 'A1a1b1c2C1c2a3');
36+
console.log(stringCompression('AabccCccaaa') === 'AabccCccaaa');
37+
// Explanation: 'A1a1b1c2C1c2a3' length is longer than original string so returns original string
38+
console.log(stringCompression('x') === 'x');

0 commit comments

Comments
 (0)