Longest Repeated Letter Block with Adjacent Swaps
Updated
Problem statement
You are given a string s and a non-negative integer k. In one operation, swap the characters at two adjacent positions.
After at most k operations, choose any letter and any contiguous block consisting only of that letter. Return the maximum possible length of such a block.
You may leave some occurrences of the chosen letter outside the block. Characters cannot be inserted, deleted, or replaced, and each adjacent swap costs one operation. You do not have to use the entire budget.
Practice domain
For this exercise, assume s is nonempty and contains only lowercase English letters. The result is the block length, not a transformed string.
Function
longestRepeatedBlock(s: String, k: int) → intExamples
Example 1
s = "aabaa"k = 2return = 4Swap the middle b rightward twice: aabaa → aaaba → aaaab. The four as form one block. No block can have length five because the string has only four copies of a.
Example 2
s = "ababa"k = 1return = 2One swap can produce aabba, whose longest block has length two. Gathering all three as requires two adjacent swaps, so length three is unavailable.
Example 3
s = "abcabc"k = 0return = 1With no swaps allowed, every existing block has length one.
Constraints
- For this exercise, assume
1 <= s.length <= 2000. scontains only characters fromathroughz.- For this exercise, assume
0 <= k <= 10^9.
More Intuit Online Assessment questions
- Employee Promotion OpportunitiesOA · Seen Oct 2026
- Next Pascal Row CoefficientOA · Seen Oct 2026
- Parse Router LogsOA · Seen Oct 2026
- Maximum Square-Free DivisorOA · Seen Oct 2026
- Two Tribes from Taller-Person CountsOA · Seen Oct 2026
- Shortest Path from an Encoded GraphOA · Seen Sep 2026
- Oldest and Youngest EmployeesOA · Seen Sep 2026
- Pressure-Isolation Shutdown OrderOA · Seen Sep 2026
- Submask Sum QueriesOA · Seen Sep 2026
- Alien DictionaryOA · Seen Sep 2026
Similar questions at other companies
- Maximum Equalized Substrings After One Adjacent SwapVisa · OAVisa interview questions
- Minimum Number of Non-Empty Disjoint SegmentsIBM · OAIBM interview questions
- Maximum Number of Workers with Equal Left and Right ShoesTesla · OATesla interview questions
- Tiktok Viral ChallengeTikTok · OATikTok interview questions
- Lexicographically Largest IdentifierAmazon · OA
- Lexicographically Smallest Grid Path with One Letter ChangeInfosys · OAInfosys interview questions
- Incident MonitorStripe · OAStripe interview questions
- Binary CircuitAkuna Capital · OAAkuna Capital interview questions
- Minimize Expression with ParenthesesPinterest · OAPinterest interview questions
- Word Wrap with Even JustificationSentry · OA · PHONE SCREENSentry interview questions