File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed
Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments