Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kennyxcao/5727d9538d2bffdc6fa679b5518e2845 to your computer and use it in GitHub Desktop.
Save kennyxcao/5727d9538d2bffdc6fa679b5518e2845 to your computer and use it in GitHub Desktop.
/**
* @param {string} s
* @return {string}
*/
var frequencySort = function(s) {
let freqObj = {};
let bucket = {};
let output = [];
for (var i = 0; i < s.length; i++) {
let ch = s.charAt(i);
if (!freqObj[ch]) {
freqObj[ch] = 1;
} else {
freqObj[ch] += 1;
}
}
for (let ch in freqObj) {
let freq = freqObj[ch];
if (!bucket[freq]) {
bucket[freq] = [];
}
bucket[freq].push(new Array(freq + 1).join(ch));
}
for (let i = s.length; i > 0; i--) {
if(bucket[i]) {
output.push(...bucket[i]);
}
}
return output.join('');
};
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isSubsequence = function(s, t) {
let sIndex = 0;
let tIndex = 0;
if (s.length === 0) {
return true;
}
while (tIndex < t.length) {
if (t[tIndex] === s[sIndex]) {
sIndex++;
if (sIndex >= s.length) {
return true;
}
}
tIndex++;
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment