Permutation in String

Algorithms
Medium
IBM
58.2K views

Given two strings $s1$ and $s2$, return true if $s2$ contains a permutation of $s1$, or false otherwise. Use a fixed-size Sliding Window and frequency maps.

Why Interviewers Ask This

IBM interviewers ask this to evaluate a candidate's ability to optimize space complexity beyond brute force. They specifically test if you can recognize that checking all permutations is inefficient and instead apply the sliding window technique with frequency maps. This demonstrates mastery of string manipulation, hash map operations, and algorithmic efficiency under constraints.

How to Answer This Question

1. Clarify requirements: Confirm if case sensitivity matters or if only lowercase English letters are used, as IBM values precise problem scoping. 2. Analyze constraints: Note that since s1 length determines the window size, you do not need to generate permutations. 3. Define the strategy: Propose using a fixed-size sliding window over s2 equal to s1's length. 4. Initialize structures: Create two frequency arrays or maps for s1 and the current window in s2. 5. Execute the slide: Iterate through s2, updating the window by adding the new character and removing the one leaving the window. 6. Compare states: Check if the current window map matches the s1 map exactly after each update. 7. Optimize: Discuss how comparing maps can be optimized by tracking a match count variable to avoid O(26) comparisons on every step.

Key Points to Cover

  • Recognize that generating permutations is unnecessary; focus on character counts instead
  • Correctly implement a fixed-size sliding window based on s1's length
  • Use frequency maps (or arrays) to track character occurrences efficiently
  • Optimize the comparison step to avoid linear scanning of the map on every iteration
  • Demonstrate understanding of O(N) time complexity and O(1) space complexity

Sample Answer

To solve the Permutation in String problem efficiently, I would first clarify that we are looking for an exact substring match of any permutation of s1 within s2. A brute-force approach generating all permutations of s1 would be too slow, likely resulting in O(n! * m) time complexity, which is unacceptable for large inputs typical at IBM. Instead, I propose a Sliding Window approach combined with frequency counting. Since the length of any valid permutation must equal s1.length(), we can maintain a fixed-size window of that length over s2. First, I would build a frequency map for characters in s1. Then, I would initialize a second map for the first window in s2. As I slide this window one character to the right, I increment the count for the incoming character and decrement the count for the outgoing character. The key optimization here is to compare these two maps. If they are identical, it means the current window contains exactly the same characters as s1 in some order, so I return true. To further optimize, I can track the number of matching character frequencies rather than re-scanning the entire map on every iteration, reducing the comparison step to constant time. This results in an overall time complexity of O(n), where n is the length of s2, and space complexity of O(1) assuming a fixed alphabet size, which is optimal for this constraint set.

Common Mistakes to Avoid

  • Attempting to generate all permutations of s1, leading to exponential time complexity
  • Using a dynamic window size instead of fixing it to the length of s1
  • Comparing substrings directly instead of comparing frequency counts
  • Failing to handle edge cases where s1 is longer than s2 or strings contain special characters

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 29 IBM questions