Created
May 14, 2018 17:00
-
-
Save MichaelDimitras/83e8e0c5097e890109852ed7a87cb20f to your computer and use it in GitHub Desktop.
Interview Questions
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
/** | |
* @param {string} s | |
* @param {string} t | |
* @return {boolean} | |
*/ | |
var isSubsequence = function(s, t) { | |
if(!s.length) { | |
return true; | |
} | |
let threads = []; | |
for (var i = 0; i < t.length; i++) { | |
if (t[i] === s[0]) { | |
threads.push(t[i]); | |
} | |
for (let j = 0; j < threads.length; j++) { | |
if(t[i] === s[threads[j].length]) { | |
threads[j] += t[i]; | |
if (threads[j].length === s.length) { | |
return true; | |
} | |
} | |
} | |
} | |
return false; | |
}; |
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
var frequencySort = function(s) { | |
let charCounts = {}; | |
for(let i = 0; i < s.length; i++) { | |
if(charCounts[s[i]]) { | |
charCounts[s[i]] += 1; | |
} else { | |
charCounts[s[i]] = 1; | |
} | |
} | |
let freqs = Object.keys(charCounts); | |
freqs.sort((a,b) => { | |
return charCounts[b] - charCounts[a]; | |
}) | |
freqs = freqs.map(item => { | |
let accumulator = ''; | |
for(i = 0; i < charCounts[item]; i++) { | |
accumulator += item; | |
} | |
return accumulator; | |
}); | |
return freqs.join('') | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment