Detect permutation in string
Last updated: October 4, 2025
Quick Overview
Given two strings, s1 and s2, determine if any permutation of s1 exists as a substring in s2. The function should return true if such a permutation is found, and false otherwise. The input strings can contain lowercase letters only, and the output should be a boolean value.
JPMorgan
October 4, 202531
14
3,371 solved
Given two strings, s1 and s2, determine if any permutation of s1 exists as a substring in s2. The function should return true if such a permutation is found, and false otherwise. The input strings can contain lowercase letters only, and the output should be a boolean value.
JPMorgan uses this problem in the Technical Screen to evaluate your algorithmic thinking. They expect you to discuss multiple approaches, analyze trade-offs between them, and implement the optimal solution with clean, readable code.
What the Interviewer Expects
- Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
- Discuss multiple approaches and trade-offs before coding
- Implement an optimal solution with clean, production-quality code
- Handle all edge cases including boundary conditions and invalid input
- Optimize both time and space complexity with clear justification
- Test your solution systematically with well-chosen examples
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you parallelize this solution?
- How would your solution change if the input was sorted?
- What happens if the input contains duplicates?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Problem Analysis
To determine if any permutation of string s1 exists as a substring in s2, we realize that the problem can be effectively solved using the sliding window technique along with **frequency counti...
Approach
-
Character Count for
s1: First, create a frequency count of the characters ins1. For example, ifs1 = 'abc', the frequency count will be{'a': 1, 'b': 1, 'c': 1}. -
**Sliding Window o...