-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #345 from kim-young/main
[kimyoung] WEEK 02 Solutions
- Loading branch information
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
var countBits = function (n) { | ||
let result = []; | ||
for (let i = 0; i <= n; i++) { | ||
result.push(countOneBits(i)); // count one bits for each i until n | ||
} | ||
|
||
function countOneBits(num) { // method to count one bits | ||
let oneBitCount = 0; | ||
while (num) { | ||
oneBitCount += num % 2; | ||
num = num >> 1; | ||
} | ||
return oneBitCount; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
// test cases | ||
console.log(countBits(2)); // [0,1,1] | ||
console.log(countBits(5)); // [0,1,1,2,1,2] | ||
|
||
// time - O(nlogn) | ||
// space - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Encodes a list of strings to a single string. | ||
* | ||
* @param {string[]} strs | ||
* @return {string} | ||
*/ | ||
|
||
const DELIMITER = "#"; | ||
|
||
var encode = function (strs) { | ||
let temp = ""; | ||
for (const str of strs) { // encode using the delimiter by attaching the length of the array as well | ||
temp += `${str.length}${DELIMITER}` + str; | ||
} | ||
return temp; | ||
}; | ||
|
||
/** | ||
* Decodes a single string to a list of strings. | ||
* | ||
* @param {string} s | ||
* @return {string[]} | ||
*/ | ||
var decode = function (s) { // decode using the length of array that is attached with the delimiter | ||
let result = []; | ||
let i = 0; | ||
while (i < s.length) { | ||
let j = i; | ||
while (s[j] !== DELIMITER) j++; | ||
const len = Number(s.slice(i, j)); | ||
const word = s.slice(j + 1, j + len + 1); | ||
result.push(word); | ||
i = j + len + 1; | ||
} | ||
return result; | ||
}; | ||
|
||
/** | ||
* Your functions will be called as such: | ||
* decode(encode(strs)); | ||
*/ | ||
|
||
// test cases | ||
console.log(["Hello", "World"]); | ||
|
||
// time - O(n) - iterate through the list of strings once | ||
// space - O(n) - stores the strings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// First Approach - hash map | ||
var isAnagram = function (s, t) { | ||
let map = {}; | ||
for (const char of s) { | ||
// count character occurence of s | ||
map[char] ? map[char]++ : (map[char] = 1); | ||
} | ||
for (const char of t) { | ||
// compare character occurence of t to the map object | ||
if (map[char]) { | ||
map[char]--; // decrement each time | ||
} else { | ||
return false; // if there's a new character, return false | ||
} | ||
} | ||
for (const el of Object.values(map)) { | ||
// if all the values of the map object is 0, return true | ||
if (el !== 0) return false; // otherwise return false; | ||
} | ||
return true; | ||
}; | ||
|
||
// test cases | ||
console.log(isAnagram("anagram", "nagarma")); | ||
console.log(isAnagram("rat", "car")); | ||
|
||
// time - O(s + t) - iterate through both input strings | ||
// space - O(n) - map obj | ||
|
||
//Second Approach - sorted strings | ||
var isAnagram = function (s, t) { | ||
return s.split("").sort().join("") === t.split("").sort().join(""); | ||
}; | ||
|
||
// test cases | ||
console.log(isAnagram("anagram", "nagarma")); | ||
console.log(isAnagram("rat", "car")); | ||
|
||
// time - O(nlogn) - using sort method | ||
// space - O(1) - no extra space memory |