What is difference between Average length of codes and Average length of codewords
By definition, an alphabet is a set of things {A1, . . . , AN} that we might wish to encode.
A code C is a mapping from an alphabet {A1, . . . , AN} to a set of finite length binary strings. C(Aj) is called the codeword for symbol Aj.
The length λj of a codeword C(Aj) is the number of bits of this codeword.
Assume a code C over an alphabet of N symbols, and probabilities p(Ai). Let λi be the length of codeword C(Ai). Then, the average length of code C is the sum of p(Ai)λi over all the i. As a result in general, the average code length is bounded below by the shortest codeword length and bounded above by the longest codeword length.
And this definition looks exactly the same as the average length of codewords.
for a text that its characters get from set of n alphabets then average length of codes is O(log n).
so by this definition, the average length of an optimal code generated by Huffman algorithm is always at most ⌈log𝑛⌉ (an upper bound with the smallest integer that greater than log n, as discussed here). More often entropy is used to lower bound the average code length (ref).
However, from your explanation on solution, the context seems different --- average in your question is about sum of all codeword lengths divided by the total number. Then this should be Ω(log n) instead (a proof FYI), meaning the best case average length would be log n (not the worst case as O(log n) indicates). And it is consistent with your example of 1-bit, 2-bit, ...2n-1 bit strings.