LeetCode 2531 - Make Number of Distinct Characters Equal
The problem is a pure inequality with a geometric constraint. The condition that gives by the Pythagorean theorem in . The second condition, that the foot of the perpendicular from to plane is the orthocenter of , is much more restrictive than it first appears.
Difficulty: 🟡 Medium
Topics: Hash Table, String, Counting
Solution
Exploration
The problem is a pure inequality with a geometric constraint. The condition that $\angle BDC=\frac{\pi}{2}$ gives
$$BC^2=BD^2+CD^2$$
by the Pythagorean theorem in $\triangle BDC$. The second condition, that the foot $H$ of the perpendicular from $D$ to plane $ABC$ is the orthocenter of $\triangle ABC$, is much more restrictive than it first appears. It suggests introducing coordinates in the plane $ABC$ with origin at $H$.
Take the plane $ABC$ as $z=0$, let
$$H=(0,0,0), \qquad D=(0,0,h).$$
Since $H$ is the orthocenter, the vectors $\overrightarrow{HA},\overrightarrow{HB},\overrightarrow{HC}$ satisfy
$$\overrightarrow{HA}\cdot(\overrightarrow{HB}-\overrightarrow{HC})=0,$$
and its cyclic variants. Expanding gives
$$\overrightarrow{HA}\cdot\overrightarrow{HB} = \overrightarrow{HA}\cdot\overrightarrow{HC} = \overrightarrow{HB}\cdot\overrightarrow{HC}.$$
Thus the pairwise dot products are equal. Write
$$a=\overrightarrow{HA},\quad b=\overrightarrow{HB},\quad c=\overrightarrow{HC},$$
and denote their common pairwise dot product by $t$.
Now compute the edge lengths:
$$AB^2=|a-b|^2=|a|^2+|b|^2-2t,$$
and similarly for the others. Also
$$AD^2=|a|^2+h^2,\qquad BD^2=|b|^2+h^2,\qquad CD^2=|c|^2+h^2.$$
The right angle condition at $D$ becomes
$$(B-D)\cdot(C-D)=0,$$
hence
$$b\cdot c+h^2=0.$$
Therefore
$$t=-h^2.$$
This is the key structural identity.
Substituting,
$$AB^2=|a|^2+|b|^2+2h^2,$$
and cyclically. Summing yields
$$AB^2+BC^2+CA^2 = 2(|a|^2+|b|^2+|c|^2)+6h^2.$$
On the other hand,
$$AD^2+BD^2+CD^2 = |a|^2+|b|^2+|c|^2+3h^2.$$
Hence
$$AB^2+BC^2+CA^2 = 2(AD^2+BD^2+CD^2).$$
The desired inequality now follows immediately from Cauchy:
$$(AB+BC+CA)^2 \leq 3(AB^2+BC^2+CA^2).$$
Combining,
$$(AB+BC+CA)^2 \leq 6(AD^2+BD^2+CD^2).$$
Equality in Cauchy requires
$$AB=BC=CA.$$
We must determine whether such a tetrahedron exists under the geometric hypotheses. If $ABC$ is equilateral and $H$ is its orthocenter, then $H$ is also its circumcenter, so
$$|a|=|b|=|c|.$$
Since $a\cdot b=a\cdot c=b\cdot c=-h^2$, one can choose symmetric vectors. The condition becomes realizable. We should verify carefully that the right angle condition still holds automatically. The equality tetrahedra are exactly those with equilateral base $ABC$ and $DH\perp ABC$ through the common center.
The potentially dangerous step is the conversion of the orthocenter condition into equal pairwise dot products. That must be proved explicitly and not merely asserted.
Problem Understanding
We are given a tetrahedron $ABCD$ satisfying two geometric conditions. First, the angle $\angle BDC$ is a right angle. Second, if $H$ denotes the foot of the perpendicular from $D$ onto the plane containing $A,B,C$, then $H$ is the orthocenter of triangle $ABC$.
The statement to prove is
$$(AB+BC+CA)^2\leq 6(AD^2+BD^2+CD^2).$$
In addition, we must determine precisely when equality occurs.
This is a Type C problem. The task is to establish an inequality together with a complete characterization of the equality case.
The geometric data involve a tetrahedron, orthogonality in space, and the orthocenter of a planar triangle. A direct synthetic treatment is awkward because the expression mixes edge lengths from the base triangle with edges incident to the vertex $D$. The core difficulty is uncovering a hidden algebraic relation connecting these two families of lengths.
The decisive insight is that the orthocenter condition becomes extremely simple in vector coordinates centered at $H$. After translating the geometry into dot products, the right angle condition at $D$ forces a common value for all pairwise dot products among the vectors from $H$ to $A,B,C$. This produces an exact identity relating
$$AB^2+BC^2+CA^2$$
to
$$AD^2+BD^2+CD^2,$$
after which the desired inequality follows from the quadratic mean inequality.
Equality should occur precisely when
$$AB=BC=CA,$$
because the final step uses the inequality
$$(x+y+z)^2\leq 3(x^2+y^2+z^2),$$
whose equality condition is equality of all three variables.
Proof Architecture
The proof will proceed through three lemmas.
Lemma 1
If $H$ is the orthocenter of triangle $ABC$, and
$$a=\overrightarrow{HA},\qquad b=\overrightarrow{HB},\qquad c=\overrightarrow{HC},$$
then
$$a\cdot b=a\cdot c=b\cdot c.$$
The reason is that the altitude conditions give orthogonality relations such as
$$a\cdot(b-c)=0.$$
Lemma 2
If the plane $ABC$ is represented by $z=0$, with
$$H=(0,0,0),\qquad D=(0,0,h),$$
then the condition $\angle BDC=\frac{\pi}{2}$ implies
$$b\cdot c=-h^2.$$
This follows by expanding
$$(B-D)\cdot(C-D)=0.$$
Lemma 3
Under the hypotheses of the problem,
$$AB^2+BC^2+CA^2 = 2(AD^2+BD^2+CD^2).$$
This comes from expressing all edge lengths in terms of $a,b,c,h$ and substituting the common dot product value from the previous lemmas.
The main argument then applies
$$(x+y+z)^2\leq 3(x^2+y^2+z^2)$$
to the numbers $AB,BC,CA$.
The most delicate step is Lemma 1. A careless argument might treat the orthocenter condition informally and overlook the exact vector identities required later.
Solution
Choose Cartesian coordinates so that the plane $ABC$ is the plane $z=0$, the orthocenter $H$ is the origin, and
$$D=(0,0,h)$$
for some real number $h>0$.
Write
$$A=a,\qquad B=b,\qquad C=c,$$
where $a,b,c\in\mathbb{R}^2\times{0}$ are regarded as vectors in the plane $z=0$.
Lemma 1
The vectors $a,b,c$ satisfy
$$a\cdot b=a\cdot c=b\cdot c.$$
Proof
Since $H$ is the orthocenter of triangle $ABC$, the altitude through $A$ is perpendicular to the side $BC$. Because $H$ lies on this altitude,
$$AH\perp BC.$$
In vector form,
$$a\cdot(b-c)=0.$$
Hence
$$a\cdot b=a\cdot c.$$
Similarly, from
$$BH\perp CA,$$
we obtain
$$b\cdot(c-a)=0,$$
hence
$$b\cdot c=b\cdot a.$$
Combining these equalities gives
$$a\cdot b=a\cdot c=b\cdot c.$$
∎
This establishes that all pairwise dot products among $a,b,c$ are equal, and bypasses the tempting but insufficient shortcut of arguing only from geometric symmetry.
Lemma 2
The condition $\angle BDC=\frac{\pi}{2}$ implies
$$b\cdot c=-h^2.$$
Proof
Since $\angle BDC$ is a right angle,
$$(B-D)\cdot(C-D)=0.$$
Substituting
$$B=b,\qquad C=c,\qquad D=(0,0,h),$$
gives
$$(b-(0,0,h))\cdot(c-(0,0,h))=0.$$
Because $b$ and $c$ lie in the plane $z=0$,
$$b\cdot(0,0,h)=0,\qquad c\cdot(0,0,h)=0.$$
Therefore
$$b\cdot c+h^2=0,$$
so
$$b\cdot c=-h^2.$$
∎
This identifies the common dot product explicitly, and avoids the common error of forgetting the contribution of the vertical coordinate.
Lemma 3
One has
$$AB^2+BC^2+CA^2 = 2(AD^2+BD^2+CD^2).$$
Proof
By Lemma 1 and Lemma 2,
$$a\cdot b=a\cdot c=b\cdot c=-h^2.$$
Now
$$AB^2=|a-b|^2.$$
Expanding,
$$AB^2=|a|^2+|b|^2-2a\cdot b =|a|^2+|b|^2+2h^2.$$
Similarly,
$$BC^2=|b|^2+|c|^2+2h^2,$$
and
$$CA^2=|c|^2+|a|^2+2h^2.$$
Adding these three identities,
$$AB^2+BC^2+CA^2 = 2(|a|^2+|b|^2+|c|^2)+6h^2.$$
Next,
$$AD^2=|a-(0,0,h)|^2=|a|^2+h^2,$$
and similarly,
$$BD^2=|b|^2+h^2,\qquad CD^2=|c|^2+h^2.$$
Hence
$$AD^2+BD^2+CD^2 = |a|^2+|b|^2+|c|^2+3h^2.$$
Multiplying by $2$ yields
$$2(AD^2+BD^2+CD^2) = 2(|a|^2+|b|^2+|c|^2)+6h^2.$$
Comparing with the earlier expression proves
$$AB^2+BC^2+CA^2 = 2(AD^2+BD^2+CD^2).$$
∎
This converts the geometric hypotheses into an exact quadratic identity, and avoids the false shortcut of estimating the left side term by term.
We now prove the required inequality.
For any real numbers $x,y,z$,
$$(x+y+z)^2\leq 3(x^2+y^2+z^2),$$
with equality if and only if
$$x=y=z.$$
Applying this to
$$x=AB,\qquad y=BC,\qquad z=CA,$$
gives
$$(AB+BC+CA)^2 \leq 3(AB^2+BC^2+CA^2).$$
By Lemma 3,
$$3(AB^2+BC^2+CA^2) = 6(AD^2+BD^2+CD^2).$$
Therefore
$$(AB+BC+CA)^2 \leq 6(AD^2+BD^2+CD^2).$$
It remains to determine the equality case.
Equality holds if and only if equality holds in
$$(x+y+z)^2\leq 3(x^2+y^2+z^2),$$
hence if and only if
$$AB=BC=CA.$$
Thus the base triangle $ABC$ must be equilateral.
Conversely, suppose $ABC$ is equilateral. Since $H$ is the orthocenter of $ABC$, it is also the circumcenter. Hence
$$|a|=|b|=|c|.$$
From Lemma 1,
$$a\cdot b=a\cdot c=b\cdot c.$$
Lemma 2 then ensures the right angle condition at $D$, and the previous argument shows equality indeed occurs.
Hence equality holds precisely for those tetrahedra whose face $ABC$ is equilateral.
Therefore,
$$\boxed{(AB+BC+CA)^2\leq 6(AD^2+BD^2+CD^2)}$$
with equality if and only if $ABC$ is equilateral.
Verification of Key Steps
The first delicate step is the derivation
$$a\cdot b=a\cdot c=b\cdot c.$$
Re-derive it independently. Since $AH$ is perpendicular to $BC$,
$$(a-0)\cdot(b-c)=0.$$
Expanding gives
$$a\cdot b-a\cdot c=0.$$
Likewise,
$$b\cdot(c-a)=0$$
gives
$$b\cdot c=b\cdot a.$$
Together,
$$a\cdot b=a\cdot c=b\cdot c.$$
A careless argument might claim these equalities from “symmetry of the orthocenter”, but no symmetry exists in a general acute triangle.
The second delicate step is the computation from the right angle condition. Starting directly from
$$(B-D)\cdot(C-D)=0,$$
we obtain
$$b\cdot c-b\cdot(0,0,h)-c\cdot(0,0,h)+h^2=0.$$
Because $b$ and $c$ lie in the plane $z=0$, the middle two terms vanish. Hence
$$b\cdot c=-h^2.$$
The common mistake here is to forget the final $h^2$ term and conclude incorrectly that $b\cdot c=0$.
The third delicate step is the equality condition. Equality in
$$(x+y+z)^2\leq 3(x^2+y^2+z^2)$$
occurs exactly when
$$x=y=z.$$
Thus
$$AB=BC=CA.$$
No additional equality conditions arise from Lemma 3 because that identity is exact. A careless proof might overlook this and impose unnecessary constraints on $AD,BD,CD$.
Alternative Approaches
A synthetic approach is possible using classical identities for a triangle with orthocenter $H$. One may express
$$AB^2=AH^2+BH^2-2,AH\cdot BH,$$
interpret the common dot product geometrically, and derive the same quadratic identity through orthogonal projections. This proof remains conceptually similar to the vector solution but hides the computations inside geometric language.
Another route uses barycentric coordinates relative to triangle $ABC$. Since $H$ is the orthocenter and $DH\perp ABC$, the point $D$ lies on the normal through the orthocenter. The right angle condition at $D$ translates into a relation among the circumradius and the altitude length. One can then derive explicit formulas for all six edges and reduce the problem to an inequality in three variables. This method is substantially longer and obscures the central structure.
The coordinate-vector method is preferable because the orthocenter condition becomes linear, the right angle condition becomes a single dot product identity, and the desired inequality reduces immediately to a standard quadratic inequality.