Maximize Repeated Substring Length
Updated
Problem statement
You are given a string s made of lowercase English letters and question marks (?). Replace every question mark with any lowercase letter you like.
A repeat is a substring (a block of characters next to each other) of even length whose first half is exactly the same as its second half. For example, abab and zz are repeats, but abba is not.
Choose the replacements so that the longest repeat in the final string is as long as possible, and return that length. If no repeat can be made, return 0.
Function
maximizeRepeatedSubstringLength(s: String) → intExamples
Example 1
s = "a??a"return = 4Replace both question marks with a to get aaaa. Its first half aa equals its second half aa, so the whole string is a repeat of length 4, the longest possible.
Constraints
1 ≤ s.length ≤ 2000scontains only lowercase English letters and?.
More Amazon interview questions
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Diagonal Traversal of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Spread Through a GridPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Consistent-Hash Packet RouterPHONE SCREEN · Seen Nov 2026
- Count Connected ComponentsPHONE SCREEN · Seen Nov 2026
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
Similar questions at other companies
- Lexicographically Smallest Grid Path with One Letter ChangeInfosys · OAInfosys interview questions
- String Formation (Also for AI/ML Software Engineer Intern :)Snowflake · OASnowflake interview questions
- Longest Subsequence which is a SubstringSalesforce · OASalesforce interview questions
- Distinct MovesMeesho · OA
- Compressed Representation SizeMicrosoft · OAMicrosoft interview questions
- Find Minimum OperationsIBM · OAIBM interview questions
- String Challenge (Generic)TikTok · OATikTok interview questions
- Maximize the Lottery IDCitadel · OACitadel interview questions
- Sum of All ValuesFlexport · OAFlexport interview questions
- Maximum Crayon Removal ScoreWells Fargo · OAWells Fargo interview questions