Skip to content

Commit 75d4e33

Browse files
committed
Stretch file
1 parent 3e81ef8 commit 75d4e33

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
function isValidAlphaAbbreviation(word: string, abbr: string): boolean {
2+
let i = 0; // pointer for word
3+
let j = 0; // pointer for abbr
4+
let prevWasSkip = true;
5+
6+
7+
8+
// Helper: convert 'a'..'z' → 1..26
9+
function alphaToNum(ch: string): number {
10+
return ch.charCodeAt(0) - 'a'.charCodeAt(0) + 1;
11+
}
12+
13+
// Helper: check if this is a skip letter
14+
function isSkipLetter(ch: string): boolean {
15+
if (ch.length !== 1) return false;
16+
const num = alphaToNum(ch);
17+
return num >= 1 && num <= 26;
18+
}
19+
20+
21+
while (j < abbr.length && i < word.length) {
22+
const ch = abbr[j];
23+
if (!prevWasSkip) {
24+
25+
if (isSkipLetter(ch)) {
26+
const skipCount = alphaToNum(ch);
27+
if (i + skipCount > word.length) {
28+
return false; // Skip goes beyond word length
29+
}
30+
31+
32+
prevWasSkip = true;
33+
j++;
34+
i+= skipCount; // Skip ahead in abbr
35+
36+
continue;
37+
} else {
38+
return false; // Invalid abbreviation
39+
}
40+
}
41+
42+
else if (word[i] === ch) {
43+
i++;
44+
j++;
45+
prevWasSkip = false;
46+
} else {
47+
return false; // Mismatch found
48+
}
49+
}
50+
51+
52+
return true;
53+
}
54+
55+
// --- test harness skeleton ---
56+
console.log('a'.charCodeAt(0)); // What number do you get?
57+
console.log('b'.charCodeAt(0)); // What about this one?
58+
console.log('z'.charCodeAt(0)); // And this?
59+
console.log(isValidAlphaAbbreviation("internationalization", "imzdn")); // expect true
60+
console.log(isValidAlphaAbbreviation("substitution", "sjn")); // expect true
61+
console.log(isValidAlphaAbbreviation("substitution", "seen")); // expect false
62+
console.log(isValidAlphaAbbreviation("test", "ab")); // expect false

0 commit comments

Comments
 (0)