Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions chapter01/1.2 - Check Perm/checkPermute.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Time O(n log n) for Sorting
// Space O(n) for sorting
var checkPermute = function(stringOne, stringTwo) {
// if different lengths, return false
if (stringOne.length !== stringTwo.length) {
Expand All @@ -11,9 +13,48 @@ var checkPermute = function(stringOne, stringTwo) {
}
};

/**
* @param: {string} str1
* @param: {string} str2
* @return: {boolean}

Time O(n) & Space O(n)

Iterate over the initial input string
Throw all of the elements into a frequency counter
Iterate over the second input string

Then subtract 1 from each element
Iterate over the hashTable and make sure all of the values are zero

**/

const checkPermute = function(str1, str2) {
const freq = str1.split('').reduce((a,c) => {
a[c] = (a[c] || 0 ) + 1
return a
}, {})

for (let chr of str2) {
freq[chr]--
}

for (let key in freq) {
if (freq[key] !== 0) {
return false
}
}

return true
}

// Tests
console.log(checkPermute('aba', 'aab'), true);

console.log(checkPermute('aba', 'aaba'), false);
console.log(checkPermute('aba', 'aa'), false);

console.log(checkPermute('aba', 'aa'), false);
console.log(checkPermute('', ''), true)
console.log(checkPermute('abc', 'cba'), true)
console.log(checkPermute('', 'abc'), false)
console.log(checkPermute('aaaaaaaaaaaaaaaaaa', 'aaaaaaaaaaaaaaaaaa'), true)
console.log(checkPermute('aaaaaaa1aaaaaaaaaa', 'aaaaaaaaaaaaaaaaaa'), false)