DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord account linking.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Vanishing String

Back to problem

Editorial

For a string TTT, define the following two values.

D0(T)D_0(T)D0​(T) is the number of 0s at relative odd positions of TTT minus the number of 0s at relative even positions.

D1(T)D_1(T)D1​(T) is the number of 1s at relative odd positions of TTT minus the number of 1s at relative even positions.

If a string is vanishing, then necessarily

D0(T)=D1(T)=0.D_0(T)=D_1(T)=0.D0​(T)=D1​(T)=0.

When two equal adjacent characters are deleted, one of them is at an odd position of the current string and the other is at an even position. Therefore, the odd-position count and the even-position count of that character both decrease by one, so D0D_0D0​ and D1D_1D1​ do not change. Both values are 000 for the empty string, proving necessity.

Conversely, if D0(T)=D1(T)=0D_0(T)=D_1(T)=0D0​(T)=D1​(T)=0, then TT always vanishes. If is empty, this is trivial. If is nonempty and has no equal adjacent characters, then it must alternate as or . In such a string, one character appears only at odd positions and the other only at even positions, so both and cannot be .

Thus, every nonempty such TTT has two equal adjacent characters. Deleting them preserves D0=D1=0D_0=D_1=0D0​=D1​=0 and reduces the length by . Repeating this process eventually produces the empty string.

Hence, a substring is vanishing if and only if D0=D1=0D_0=D_1=0D0​=D1​=0.

Now give position iii sign +1+1+1 if iii is odd and sign −1-1−1 if iii is even. Define the prefix state

Pi=(p0,i,p1,i),P_i=(p_{0,i},p_{1,i}),Pi​=(p0,i​,p

where p0,ip_{0,i}p0,i​ is the sum of these signs over all 0s in positions 111 through iii, and p1,ip_{1,i}p is defined similarly for s.

For a substring SlSl+1⋯SrS_lS_{l+1}\cdots S_rSl​Sl+1​⋯Sr​, its relative position parity is either the same as the original parity or completely reversed, depending on the parity of . Reversing every sign does not change whether both values are zero.

Therefore,

SlSl+1⋯SrS_lS_{l+1}\cdots S_rSl​Sl+1​⋯Sr​

is vanishing if and only if

Pr=Pl−1.P_r=P_{l-1}.Pr​=Pl−1​.

Scan the string from left to right while counting how many times each prefix state has appeared. If the current state has already appeared ccc times, then exactly ccc new vanishing substrings end at the current position. Initially, the empty prefix state (0,0)(0,0)(0,0) has appeared once.

Using a map gives O(Nlog⁡N)O(N\log N)O(NlogN) time, while a hash map gives expected O(N)O(N)O(N) time. The memory complexity is O(N)O(N)O(N).

The number of substrings is at most

N(N+1)2.\frac{N(N+1)}{2}.2N(N+1)​.

Since N≤200 000N\leq200\,000N≤200000, the answer always fits in a signed 64-bit integer.

Solution written by GPT5.6

T
TTT
TTT
0101
⋯\cdots⋯
1010
⋯\cdots⋯
D0D_0D0​
D1D_1D1​
000
22
2
1,i
​
)
,
1,i
​
1
lll