DOJ logoDOJ
Problems
Recently added problemsSaved problems
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-backed certification.

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

Making a Pelindrome

Back to problem

Editorial

Let the completed string be XXX.

If the prefix X[1..i]X[1..i]X[1..i] of length iii is a palindrome, its first and last characters must be equal. Therefore, X1=XiX_1=X_iX1​=Xi​ for every iii (1≤i≤N1 \leq i \leq N1≤i≤N). Thus, every character of XXX must be the same.

It remains to inspect the uppercase letters already fixed in SSS.

  • If there is no fixed uppercase letter, any of the 26 uppercase letters can be the common character, so the answer is 262626.
  • If all fixed uppercase letters are equal, the common character is uniquely determined, so the answer is 111.
  • If two different fixed uppercase letters exist, making all characters equal is impossible, so the answer is 000.

Scanning each test case once is sufficient. The time complexity is O(N)O(N)O(N), and the extra space complexity is O(1)O(1)O(1).