Maximize the Lottery ID
Learn this problemProblem statement
🐣 Source note (2026-07-17): The sample lottery ID was corrected from uppercase I to lowercase l, and the source constraints were restored. The judged core task matches the visible source at about 99%.
The newspaper HackerRankNews distributed lottery tickets to everyone, and the winning lottery ID is described in the paper denoted by winnerID.
In order to win the lottery, a customer having a lottery ID denoted by lotteryID needs to maximize the length of the longest common subsequence of the strings lotteryID and winnerID. To do so, the customer can perform up to k operations. In each operation, the customer can change any character in lotteryID either to its next or previous character.
Find the maximum possible length of the longest common subsequence after at most k such operations.
Note:
- Operations can be performed only on
lotteryID, not onwinnerID. - The next character of a given character
chis the character that comes just after it in the alphabet. Similarly, the previous character is the character that comes just before it in the alphabet. For example, the next character of 'm' is 'n' and the previous is 'l'. For 'z' the next character is 'a' and for 'a' the previous character is 'z'.
Function
maximizeLotteryID(lotteryID: String, winnerID: String, k: int) → intExamples
Example 1
lotteryID = "fpelqanxyk"winnerID = "hackerrank"k = 6return = 7- Select
i = 0and movelotteryID[0]to its next character:"gpelqanxyk". - Select
i = 0again:"hpelqanxyk". - Select
i = 2and movelotteryID[2]to its previous character:"hpdlqanxyk". - Select
i = 2again:"hpclqanxyk". - Select
i = 3and movelotteryID[3]to its previous character:"hpckqanxyk". - Select
i = 4and movelotteryID[4]to its next character:"hpckranxyk".
winnerID have a longest common subsequence of length 7; for example, "hckrank". Therefore, the maximum possible length after at most k operations is 7.Constraints
1 <= lotteryID.length, winnerID.length <= 1000 <= k <= 200
More Citadel problems
- Minimum Changes for a Periodic PalindromeOA · Seen Jul 2026
- Minimum Image Processing CostOA · Seen Jul 2026
- Minimum Path Sum to Target in Binary TreePHONE SCREEN · Seen Apr 2026
- Minimum Time to Process RequestsOA · Seen Mar 2026
- Process SchedulingOA · Seen Mar 2026
- Social Media SuggestionsSeen May 2025
- Best Sum Downward Tree PathSeen May 2025
- Price CheckSeen Feb 2025