Replace '?' characters to minimize a string's total penalty, returning the lexicographically smallest result.
There are 26 categories and each data entry is labeled with one, entries[i] being a lowercase English letter; some entries[i] are '?', meaning unknown and must be replaced with a lowercase letter. The penalty of index i is the number of indices before i that have the same character as entries[i]. Examples: "hello" -> [0,0,0,1,0]; "abc" -> [0,0,0]; "aaccbbc" -> [0,1,0,1,0,1,2]. Task: replace all '?' so the total penalty is minimum; if multiple strings achieve the minimum, return the lexicographically smallest. Function: string calculateOptimalEntries(string entries). Constraints: 1 <= |entries| <= 10^5; at least one '?'; all characters lowercase letters or '?'. Sample: entries = "abcd?" -> "abcde" (zero penalty, lexicographically smallest).
Format · Amazon OA.


