Skip to content

Commit ae518fc

Browse files
feat: created a file and folder for the stretch assignment, where i implemented the code
1 parent 6ac4dac commit ae518fc

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function isValidAlphaAbbreviation(word:string, abbreviation:string):boolean{
2+
let i:number = 0; //keep the index of the word
3+
for(let j=0; j<abbreviation.length;j++){
4+
const abbrChar=abbreviation[j];
5+
6+
// We add 1 to shift the range, 'a'.charCodeAt(0) - 'a'.charCodeAt(0) = 0 ,
7+
// because 97-97 = 0.
8+
// Adding the '1' shifts it to a===1, which is what we want.
9+
10+
const skip =abbrChar.charCodeAt(0) - 'a'.charCodeAt(0) + 1 ;
11+
if(word[i] === abbrChar){
12+
i++; //if the current word letter and the current abbrchar match match go to the next letter
13+
}else{
14+
i+=skip; // Otherwise, treat abbrChar as a letter abbreviation and skip that many characters
15+
}
16+
}
17+
return i === word.length;
18+
}
19+
20+
console.log(isValidAlphaAbbreviation("substitution", "sjn")); //true
21+
console.log(isValidAlphaAbbreviation("test", "ab")); //false

0 commit comments

Comments
 (0)