---
title: Double or One Thing
categories: [Technical Logs, Individual]
tags: []
---
> This page is still in WIP. A lot of information is missing or not reviewed.
{: .prompt-danger }
---
Try this problem by yourself first! Check the [Code Jam Page](https://codingcompetitions.withgoogle.com/codejam/round/0000000000877ba5/0000000000aa8e9c).
## Overview
> A HIGH LEVEL SUMMARY THAT EVERY ENGINEER SHOULD UNDERSTAND AND CAN NAVIGATE THROUGH YOUR PROBLEM-SOLVING PROCESS.
O O O
o o o
...
## Context
Let's summarize the problem. Take a string of uppercase English characters. We can highlight any of its letters, possibly all or none of them. Every highlighted letter will be duplicated, every non-highlighted letter will stay as is. We'll give an example with all of its highlighting possibilities.
| RIDE -> RIDE | RIDE -> RIDDE | RIDE -> RIDEE | RIDE -> RIDDEE |
| RIDE -> RRIDE | RIDE -> RRIDDE | RIDE -> RRIDEE | RIDE -> RRIDDEE |
| RIDE -> RIIDE | RIDE -> RIIDDE | RIDE -> RIIDEE | RIDE -> RIIDDEE |
| RIDE -> RRIIDE | RIDE -> RRIIDDE | RIDE -> RRIIDEE | RIDE -> RRIIDDEE |
Now, with all of our results, we must sort alphabetically all of them. We would obtain the following result:
| 1. RIDDE | 5. RIIDDE | 9. RRIDDE | 13. RRIIDDE |
| 2. RIDDEE | 6. RIIDDEE | 10. RRIDDEE | 14. RRIIDDEE |
| 3. RIDE | 7. RIIDE | 11. RRIDE | 15. RRIIDE |
| 4. RIDEE | 8. RIIDEE | 12. RRIDEE | 16. RRIIDEE |
As a result, **RIDDE** is the first word that appears. This is exactly the word we're looking for.
### How can we sort in alphabetical order?
Let's compare two words: *book* and *boat*. Alphabetically, which one comes first? And why? Let's make a procedure.
1. Compare each letter from left to right. Find the first ones that differ.
| **B** ook - **B** oat | *b* **O** ok - *b* **O** at | *bo* **O** k - *bo* **A** t |
| They're the same. (**b**) | They're the same. (**o**) | They differ. (**o** =/= **a**) |
1. The letter that differs decides which word comes first.
| Book > Boat |
|:-:|
| *Book* comes after *boat*, because *o* comes after *a* in the alphabet. |
1. What if one word is a subset of the other? The smallest word will come first, then.
| **C** an - **C** annot | *c* **A** n - *c* **A** nnot | *ca* **N** - *ca* **N** not | *can* - *can* **N** ot |
| Same letter. (**c**) | Same letter. (**a**) | Same letter. (**n**) | Differ. (**_** =/= **n**) |
| Can < Cannot |
|:-:|
| *Can* comes before *cannot*, because *can* is a subset of *cannot*. |
This procedure will be essential to understand the solution.
O O O
o o o
...
## Solution
The big picture of the solution is as following:
1. Walk through the character string letter by letter.
1. If the current letter comes before the next letter in the alphabet, **highlight it**.
1. If the current letter comes after the next letter in the alphabet, **don't highlight it**.
1. If the current letter is the same as the next letter, keep walking until you find a letter different from the current one. Highlighting all the letters or none of them will depend on if the current letter comes before or after the first different one, respectively.
1. The last letter **will never be highlighted**.
We illustrate two examples following the procedure.
#### RIDE
1. **R**IDE -> The *R* comes after the *I*. Hence, we won't highlight the *R*.
1. R**I**DE -> The *I* comes after the *D*. Therefore, we won't highlight the *I*.
1. RI**D**E -> The *D* comes before the *E*. Now we will highlight the *D*.
1. RID**E** -> We won't highlight the *E* because it is the last letter.
The resulting string is RIDE. Just as we saw in the [context section](#context), this is the desired output.
#### FEEELING
1. **F**EEELING -> *F* > *E*. Therefore, *F* is not highlighted.
1. F**E**EELING -> *E* = *E*. We'll keep walking.
1. F**E**EELING -> *E* = *E*. Keep on walkiiiing.
1. F**E**EELING -> *E* < *L*. Hence, we'll highlight all *E*'s.
1. FEEE**L**ING -> *L* > *I*. We won't highlight the *L*.
1. FEEEL**I**NG -> *I* < *N*. The *I* must be highlighted.
1. FEEELI**N**G -> *N* > *G*. You better not touch the *N*. Don't highlight it!
1. FEEELIN**G** -> *G* > *_*. Last letter must never be highlighted. >:(
Therefore, the solution is FEEELING. Is this the right solution? Lets use the brute force way! This means: listing all highlighting possibilities and sorting them. We obtain:
| 1. FEEELING -> FEEEEEELIING | 5. FEEELING -> FEEEEEELING | ... | 253. FEEELING -> FFEEELLING |
| 2. FEEELING -> FEEEEEELIINGG | 6. FEEELING -> FEEEEEELINGG | ... | 254. FEEELING -> FFEEELLINGG |
| 3. FEEELING -> FEEEEEELIINNG | 7. FEEELING -> FEEEEEELINNG | ... | 255. FEEELING -> FFEEELLINNG |
| 4. FEEELING -> FEEEEEELIINNGG | 8. FEEELING -> FEEEEEELINNGG | ... | 256. FEEELING -> FFEEELLINNGG |
Source: Trust me, bro.
That's the right solution! But wait... Why is it the right solution? Why does the procedure works?
### Why does it works?
In this work we will divide the explanation in two parts: with and without repeating consecutive letters. We will start with no repeating letters, as it is the easiest one.
#### Without repeating letters
In the procedure we walk through each character of the string. For each one we decide if we highlight it or not. Why does the procedure work this way? Let's review the example of RIDE more closely. We will start with the letter **R** and see what happens if we highlight it and what happens if we don't.
RIDE
| Highlighting? | After doubling | Sorting alphabetically | Which one won? |
|:-|:-|:-|:-|
| Highlighted | RIDE -> RRIDE | R **R** IDE | Comes second. |
| Not highlighted | RIDE -> RIDE | R **I** DE | Comes **FIRST**! |
Because *R* comes after *I*, *RRIDE* comes after *RIDE*. We better **NOT** highlight the *R*.
RIDE
| Highlighting? | After doubling | Sorting alphabetically | Which one won? |
|:-|:-|:-|:-|
| Highlighted | RIDE -> RIIDE | RI **I** DE | Comes second. |
| Not highlighted | RIDE -> RIDE | RI **D** E | Comes **FIRST**! |
Because *I* comes after *D*, *RIIDE* comes after *RIDE*. We better **NOT** highlight the *I*.
RIDE
| Highlighting? | After doubling | Sorting alphabetically | Which one won? |
|:-|:-|:-|:-|
| Highlighted | RIDE -> RIDDE | RID **D** E | Comes **FIRST**! |
| Not highlighted | RIDE -> RIDE | RID **E** | Comes second. |
Because *D* comes before *E*, *RIDDE* comes before *RIDE*. We **MUST** highlight the *D*.
RIDE
| Highlighting? | After doubling | Sorting alphabetically | Which one won? |
|:-|:-|:-|:-|
| Highlighted | RIDE -> RIDEE | RIDE **E** | Comes second. |
| Not highlighted | RIDE -> RIDE | RIDE | Comes **FIRST**! |
Because *RIDE* is a subset of *RIDEE*, *RIDEE* comes after *RIDE*. We better **NOT** highlight the *E*.
This is exactly why we compare the current letter vs the next one. If the current letter of the string appears before in the alphabet with respect to the next letter of the string, we better have more of the current letter. If the current letter appears after than the next letter, we better jump to the next one as soon as we can.
On the other hand, highlighting the last character will only make it a superset of the non-highlighted one. In the alphabetical order, we prefer our word not to be the superset one.
All of this can be represented with the following code:
```python
def first_alphabetically(my_str: str) -> str:
result = []
current = 0
while current < len(my_str) - 1:
# If the current letter comes before the next letter, highlight it.
if my_str[current] < my_str[current + 1]:
result += my_str[current] * 2
current += 1
# If the current letter comes after the next letter, don’t highlight it.
elif my_str[current] > my_str[current + 1]:
result += my_str[current]
current += 1
# If they're the same letter... We will see it later.
else:
pass
# The last letter will never be highlighted.
result += my_str[-1]
return ''.join(result)
```
{: file="solution.py"}
#### With repeating letters
O O O
o o o
...
## Alternative Solutions
> WHAT ELSE DID YOU CONSIDER WHEN COMING UP WITH THE SOLUTION ABOVE? PROS AND CONS?