Permutation in String
Updated
Problem statement
Given two strings s1 and s2, return true if some permutation of s1 appears in s2 as a substring, and false otherwise.
In other words, is there a run of s1.length consecutive characters in s2 that uses exactly the same letters as s1, each the same number of times?
Function
checkInclusion(s1: String, s2: String) → booleanExamples
Example 1
s1 = "ab"s2 = "eidbaooo"return = trueThe window ba (indices 3 and 4 of s2, 0-based) uses the same letters as ab, so a permutation of s1 appears and the answer is true.
Example 2
s1 = "ab"s2 = "eidboaoo"return = falses2 contains a b and an a, but they are never next to each other, so no window of length 2 matches.
Example 3
s1 = "abc"s2 = "ab"return = falses1 is longer than s2, so no window fits and the answer is false.
Constraints
1 <= s1.length, s2.length <= 10^4s1ands2consist of lowercase English letters.
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Longest Substring Without Repeating CharactersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Shortest Deletion for Distinct CharactersOracle · ONSITE INTERVIEWOracle interview questions
- Longest Substring Without Repeating CharactersGoogle · ONSITE INTERVIEWGoogle interview questions
- Longest Substring Without Repeating CharactersAmazon · ONSITE INTERVIEW
- Longest Substring Without Repeating CharactersInfosys · ONSITE INTERVIEWInfosys interview questions
- Longest Substring Without Repeating CharactersOmnissa · ONSITE INTERVIEWOmnissa interview questions
- Longest Substring with At Most K Distinct CharactersGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Longest Substring Without Repeating CharactersBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Sliding-Window Rate LimiterStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Longest Substring Without Repeated CharactersDatadog · ONSITE INTERVIEWDatadog interview questions