Instead of using mathematical induction in Solution 1, one can also show that \(f(2k) = f(2k+1)\) and \(f(2n+4) = f(2n+2) + f(2n)\) by construction.
To show that \(f(2k) = f(2k+1)\), we can construct a one-to-one mapping from strings of length \(2k\) to strings of length \(2k+1\). The mapping is defined as below by extending the two digits in the middle of length \(2k\) to the three digits in the middle of length \(2k+1\).
“00” -> “000” (e.g. 1089001089 -> 10890001089)
“08” -> “098” (e.g. 1089 -> 10989)
“91” -> “901” (e.g. 10891089 -> 108901089)
“99” -> “999” (e.g. 109989 -> 1099989)
To show that \(f(2n+4) = f(2n+2) + f(2n)\), we construct strings of length \(2n+4\) from strings of length \(2n+2\) and strings of length \(2n\) using the following rules:
First if the input string is in the form \(1099\ldots9989\), regardless of the length \(2n-2 or 2n\), we will generate an output string of length \(2n+4\) in the form \(1099\ldots9989\). Note that here we have a 2-to-1 mapping.
If the input string is of length \(2n+2\) and is not in the form \(1099\ldots9989\), then we construct an output string of length \(2n+4\) based on the two digits in the middle of length \(2n+2\):
“99” -> “9999” (e.g. 10999989 -> 1099999989)
“00” -> “0000” (e.g. 1089001089 -> 108900001089)
“08” -> “0998” (e.g. 108910891089 -> 10891099891089)
“91” -> “9001” (e.g. 10891089 -> 1089001089)
Note that the middle two digits are either 99 or 00 in this case.
If the input string is of length \(2n\) and is not in the form \(1099\ldots9989\), then we construct an output string of length \(2n+4\) based on the two digits in the middle of length \(2n\):
“99” -> “989109” (e.g. 10999989 -> 109989109989)
“00” -> “010890” (e.g. 1089001089 -> 10890108901089)
“08” -> “089108” (e.g. 108910891089 -> 1089108910891089)
“91” -> “910891” (e.g. 10891089 -> 108910891089)
Note that the middle two digits are either 91 or 08 in this case.
Finally we do a special handling for the unique input string of length \(2n+2\) and in the form \(1099\ldots9989\#1099\ldots9989\), there is a corresponding unique output string of length \(2n+4\) and in the form \(1099\ldots9989\#1099\ldots9989\). Note that this unique string CANNOT be generated from any string of length \(2n\) using the rules above (because no additional 1 is added).
This additional 1-to-2 mapping compensates the 2-to-1 mapping of \(1099\ldots9989\), Therefore we have \[f(2n+4) = f(2n+2) + f(2n).\]