You are given two strings
Approach: check if
Proof:
Source: https://codegolf.stackexchange.com/a/259989/
Lexicographical less (
When we say
We will prove two things for all strings
$a + b < b + a \rightarrow |b| \times a < |a| \times b$ $a + b = b + a \rightarrow |b| \times a = |a| \times b$
Before proceeding, here is an explanation of how
-
If
$a + b < b + a$ :-
Prove
$a + b < b + a \rightarrow |b| \times a < |a| \times b$ This is one of the two facts we will prove.
-
Prove
$|b| \times a < |a| \times b \rightarrow a + b < b + a$ We already have
$a + b < b + a$ . Great.
-
-
If
$a + b = b + a$ :-
Prove
$a + b < b + a \rightarrow |b| \times a < |a| \times b$ As
$a + b < b + a$ is false, the whole statement is true. -
Prove
$|b| \times a < |a| \times b \rightarrow a + b < b + a$ From
$a + b = b + a \rightarrow |b| \times a = |a| \times b$ , we have$|b| \times a = |a| \times b$ . Therefore$|b| \times a < |a| \times b$ is false and thus the whole statement is true.
-
-
If
$b + a < a + b$ :-
Prove
$a + b < b + a \rightarrow |b| \times a < |a| \times b$ As
$a + b < b + a$ is false, the whole statement is true. -
Prove
$|b| \times a < |a| \times b \rightarrow a + b < b + a$ By substituting variables in the first fact, we have
$b + a < a + b \rightarrow |a| \times b < |b| \times a$ . From there we conclude$|b| \times a < |a| \times b$ is false, thus the whole statement is true.
-
Now we can start proving the two facts. We'll prove
To prove this fact, we need to prove two intermediary lemmas: repeatA
and repeatB
.
repeatA
:
This lemma can be proved by doing induction on
Base case:
Induction step: We already have
To pull this off, we add
Now this is identical to
repeatB
:
Using a similar argument to the one used in repeatA
, we can prove this lemma. In the inductive step, however, we need to add
Both repeatA
and repeatB
have the precondition
- If
$a$ is empty,$a + b < b + a$ turns into$b < b$ , which is a false statement. - Similarly,
$b$ can't be empty.
Applying repeatA
on
Applying repeatB
on
For the sake of contradiction, let's suppose
Therefore,
As
Now we prove
We also need to prove these two facts:
repeatAEqual
:
repeatBEqual
:
We can use the same argument as the argument we used for repeatA
and repeatB
.
Before proceeding further, we consider two cases.
- If
$a$ is empty,$|b| \times a + |a| \times b = |a| \times b + |b| \times a$ reduces to$\text{“”} = \text{“”}$ , which is true. - Similarly, if
$b$ is empty then$|b| \times a + |a| \times b = |a| \times b + |b| \times a$ is true.
Now we only need to consider the case where both
Applying repeatAEqual
on
Applying repeatBEqual
on
Taking a prefix of length
The result is proved in RepeatCompare.v
.