Maximum Equalized Substrings After One Adjacent Swap
Updated
Problem statement
You are given a string s containing only L and R.
A substring is equalized when it contains the same number of L and R characters. You may swap at most one pair of adjacent characters in s.
After the optional swap, split the entire string into consecutive, nonempty equalized substrings. Return the maximum possible number of substrings in such a split. If the complete string has different total counts of L and R, return 0 because no complete equalized split is possible.
Function
maxEqualizedSubstrings(s: String) → intExamples
Example 1
s = "RLRRLLRLRL"return = 5Swap the adjacent R and L at indices 3 and 4. The string becomes RLRLRLRLRL, which splits as RL | RL | RL | RL | RL.
Example 2
s = "RL"return = 1The whole string is already one equalized substring, and no adjacent swap can create a second nonempty part.
Example 3
s = "RRLL"return = 2Swap the middle R and L to obtain RLRL, then split it as RL | RL.
Constraints
1 <= s.length <= 2 * 10^5- Every character of
sis eitherLorR. - You may perform zero or one adjacent swap.
More Visa Online Assessment questions
- Count Prime Values in SubtreesOA · Seen Nov 2026
- Lock CodeOA · Seen Nov 2026
- Minimum-Sum Tree Fill with Unit StepsOA · Seen Nov 2026
- Longest Non-Decreasing Substring After Pair SwapsOA · Seen Oct 2026
- Count Attack Vectors by Divisor RangeOA · Seen Oct 2026
- Minimum Operations to Reduce a Transaction LedgerOA · Seen Oct 2026
- Count Valid Space-Delimited WordsOA · Seen Oct 2026
- Earliest Common Meeting StartOA · Seen Oct 2026
- Maximum Index-Weighted Sum with Disjoint SwapsOA · Seen Oct 2026
- Minimum Values with Even Powers of TwoOA · Seen Sep 2026
Similar questions at other companies
- Longest Repeated Letter Block with Adjacent SwapsIntuit · OAIntuit interview questions
- Maximum Number of Workers with Equal Left and Right ShoesTesla · OATesla interview questions
- Maximum String OperationsMicrosoft · OAMicrosoft interview questions
- Minimum No-Repeat Segments After One Character RemovalSalesforce · OASalesforce interview questions
- Earliest Time to Schedule a MeetingMathWorks · OAMathWorks interview questions
- Earliest Meeting Start on a 24-Hour ClockIMC Trading · OAIMC Trading interview questions
- Minimum Cost of Every K-Capable Model SetTikTok · OATikTok interview questions
- Minimum Deletion Cost for Distinct Adjacent CharactersHSBC · OAHSBC interview questions
- Minimum Binary Swaps to a PalindromeAT&T · OAAT&T interview questions
- Robot Assembly Line ConflictsGoogle · OAGoogle interview questions