Skip to content

Instantly share code, notes, and snippets.

@MichaelDimitras
Created May 14, 2018 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelDimitras/83e8e0c5097e890109852ed7a87cb20f to your computer and use it in GitHub Desktop.
Save MichaelDimitras/83e8e0c5097e890109852ed7a87cb20f to your computer and use it in GitHub Desktop.
Interview Questions
/**
* @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;
};
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