Count Substrings Matching with One Adjacent Swap
Updated
Problem statement
You are given a lowercase text string and a non-empty lowercase pattern. Count text substrings of the pattern's length that either equal the pattern or can be made equal by swapping one pair of adjacent characters in the pattern.
Each starting position contributes at most once, even when equal adjacent characters give multiple equivalent swaps.
Function
countAdjacentSwapMatches(text: String, pattern: String) → intExamples
Example 1
text = "abcacbabc"pattern = "abc"return = 3The matching windows are abc, acb, and the final abc.
Example 2
text = "aab"pattern = "ab"return = 1Only the final window ab matches; aa cannot be obtained by one adjacent swap.
Constraints
1 <= pattern.length <= text.length <= 200000- Both strings contain only lowercase English letters.
- The answer fits in a signed 32-bit integer.
Source note: Interview prompt excerpt preserved as source evidence.
More InMobi Online Assessment questions
- Validate a Bracket SequenceOA · Seen Oct 2026
- Maximum Subarray with Start and End WindowsOA · Seen Oct 2026
- Minimum Service Cold StartsOA · Seen Oct 2026
- Range Maximum Subarray Sum QueriesOA · Seen Oct 2026
- Connected Components with Divisor ThresholdOA · Seen Aug 2026
- Maximize Impression ValueOA · Seen Aug 2026
- Supply Path OptimizationOA · Seen Aug 2026
- Delivery ServiceOA · Seen Jun 2026
- Shared InterestOA · Seen Jun 2026
- Temporal AnomaliesOA · Seen Jun 2026
Similar questions at other companies
- Lexicographically Largest IdentifierAmazon · OA
- Lexicographically Smallest Grid Path with One Letter ChangeInfosys · OAInfosys interview questions
- Incident MonitorStripe · OAStripe interview questions
- Balance a Chemical EquationMicrosoft · OAMicrosoft interview questions
- Longest Repeated Letter Block with Adjacent SwapsIntuit · OAIntuit interview questions
- Binary CircuitAkuna Capital · OAAkuna Capital interview questions
- String Formation (Also for AI/ML Software Engineer Intern :)Snowflake · OASnowflake interview questions
- Binary Sorting Rounds After FlipsVisa · OAVisa interview questions
- Minimum Numeric Code TransformationAdobe · OAAdobe interview questions
- Good SubsequencesNutanix · OANutanix interview questions