Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Returns string in order of occurrence of characters | Return bool whether s is substring of t
const frequencySort = (str) => {
let chars = {}, result = '', max = 0;
str.split('').forEach((ltr) => {
chars[ltr] ? chars[ltr]+=1 : chars[ltr] = 1;
});
return sort(chars);
};
const sort = (obj) => {
let chars = [], max = [0,0], result = '';
for(let char in obj) { chars.push([char, obj[char]]) };
while(chars.length > 0) {
for(let i = 0; i < chars.length; i +=1) {
if(chars[i][1] > max[1]) {
var pos = i;
max = chars[i];
}
}
result += max[0].repeat(max[1]);
chars.splice(pos,1);
max = [0,0];
}
return(result);
};
const isSubsequence = (s, t) => {
if (s.length === 0) return true;
let indexS = 0, indexT = 0;
while (indexT < t.length) {
if (t.charAt(indexT) === s.charAt(indexS)) {
indexS +=1;
if (indexS === s.length) return true;
}
indexT +=1;
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment