C++20 sections · 1024 units
Open in Course

Way Too Long Words - The Idea

Main approach

The trick is that abbreviation only makes sense when it shortens the word. A word of length nn abbreviates to at most 4 characters (like "a18b" where 18 is two digits). So if n10n \leq 10, the original word is short enough.

Keep it as-is. If n>10n > 10, abbreviate it. The middle count is n2n - 2 because you keep the first and last characters. String indexing gives you easy access to any character. The first character is at index 0, the last at index n1n-1.

The middle count is just arithmetic: total length minus the two characters you keep.