FastPrepCount Substrings Matching with One Adjacent Swap
Problems/Count Substrings Matching with One Adjacent Swap

Count Substrings Matching with One Adjacent Swap

InMobi logoInMobi● HardNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an InMobi new grad online assessment. The report gives the rule for counting substrings, examples with adjacent swaps, and the size limits. We added the exact method interface and the rule that each alignment counts once. Our version is about 97% the same as the reported question.

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) → int

Examples

Example 1

text = "abcacbabc"pattern = "abc"return = 3

The matching windows are abc, acb, and the final abc.

Example 2

text = "aab"pattern = "ab"return = 1

Only 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
All InMobi interview questionsSee InMobi hiring insights
Saved
class Solution {
    public int countAdjacentSwapMatches(String text, String pattern) {
        // Write your code here.
    }

}
Checking account…