function alphabetSubsequence(str) {
const chars = str.split('');
const hasDuplicate = chars.some((char, idx) => str.lastIndexOf(char) !== idx);
if (hasDuplicate) {
return false
}
const uniqueChars = Array.from(new Set(chars))
/**
* We can compare characters:
* 'b' > 'a' === true
* 'a' > 'b' === false
*/
return uniqueChars.every((cur, idx) => {
const next = chars[idx + 1] || cur;
return next >= cur;
});
}
/**
* Test Suite
*/
describe('alphabetSubsequence()', () => {
it('returns false when it has duplicate letters', () => {
// arrange
const str = 'effg';
// act
const result = alphabetSubsequence(str);
// log
console.log("result 1: ", result);
// assert
expect(result).toBe(false);
});
it('returns false when NOT in ascending a - z order', () => {
// arrange
const str = 'cdce';
// act
const result = alphabetSubsequence(str);
// log
console.log("result 2: ", result);
// assert
expect(result).toBe(false);
});
it('returns true whenno duplicates and is ascending a - z order ', () => {
// arrange
const str = 'ace';
// act
const result = alphabetSubsequence(str);
// log
console.log("result 3: ", result);
// assert
expect(result).toBe(true);
});
});