Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

567. Permutation in String

Medium46.5% Acceptance
Hash TableTwo PointersString
Asked by:
M
Microsoft
ProblemHints (6)Solutions (8)VideosCompanies (8)Notes

Problem Statement

Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise.

In other words, return true if one of s1's permutations is the substring of s2.

Example 1:

Input: s1 = "ab", s2 = "eidbaooo"
Output: true
Explanation: s2 contains one permutation of s1 ("ba").

Example 2:

Input: s1 = "ab", s2 = "eidboaoo"
Output: false

Constraints:

  • 1 <= s1.length, s2.length <= 104
  • s1 and s2 consist of lowercase English letters.
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
Apple
Y
Yandex
O
Oracle
A
Amazon
+3

Approach

The key idea in Permutation in String is to determine whether any permutation of one string appears as a contiguous substring in another string. Instead of generating all permutations (which is expensive), we can use a sliding window combined with frequency counting.

First, build a frequency map (or fixed-size array for lowercase letters) for the characters in s1. Then maintain a sliding window of the same length in s2. As the window moves forward using the two pointers technique, update the character counts for the current window.

If at any point the window's character frequency matches the frequency of s1, it means a permutation of s1 exists in s2. This approach avoids recomputation by updating counts incrementally as characters enter and leave the window.

The sliding window ensures that each character is processed only once, leading to an efficient solution with linear time complexity.

Complexity

ApproachTime ComplexitySpace Complexity
Sliding Window with Frequency CountO(n)O(1)

Video Solution Available

CrioDo

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

Obviously, brute force will result in TLE. Think of something else.

2
Hint 2

How will you check whether one string is a permutation of another string?

3
Hint 3

One way is to sort the string and then compare. But, Is there a better way?

4
Hint 4

If one string is a permutation of another string then they must have one common metric. What is that?

5
Hint 5

Both strings must have same character frequencies, if one is permutation of another. Which data structure should be used to store frequencies?

6
Hint 6

What about hash table? An array of size 26?

Ready to see the solutions?View Solutions

Solutions (8)

Sliding Window with Frequency Count

The main idea is to use a sliding window technique to compare the character frequency of a substring of s2 with the character frequency of s1. If they match, it means s2 contains a permutation of s1.

  • Initialize two frequency arrays for s1 and the sliding window in s2 of the same size.
  • Slide the window across s2, updating its frequency count and comparing it with s1's frequency count at each step.
  • If the window's frequency array matches s1's frequency array, return true, else continue sliding the window.
  • If no match is found by the end of s2, return false.

Time Complexity: O(n), where n is the length of s2, as each character in s2 is processed once.
Space Complexity: O(1), as the frequency arrays have a fixed size of 26.

CC++JavaPythonC#JavaScript
1#include <stdbool.h>
2#include <string.h>
3
4bool checkInclusion(char * s1, char * s2) {
5    int len1     
    

Explanation

This solution uses two arrays of size 26 to keep track of the frequency of each letter in s1 and the current window of s2. We first populate the arrays with the frequency of characters in s1 and the initial window of s2. Then, we slide the window over s2 one character at a time, updating the frequency array for the window and checking if it matches the frequency array of s1. If they match, a permutation exists in s2.

Character Mapping Using Hash Map

Leverage a hash map (or dictionary) to record the character count of s1 and use it to compare with segments of s2. This approach focuses on incremental updates to the map as the window slides over s2.

  • Construct a hash map for s1 storing the frequency of each character.
  • Use a sliding window to traverse s2, updating the map for each window section.
  • Compare the map for each window with the map of s1 and determine if a permutation is found.

Time Complexity: O(n), where n is the length of s2.
Space Complexity: O(1), since the hash map's size is bounded by the character set size of 26.

PythonJavaScript
1from collections import defaultdict
2
3

Video Solutions

Watch expert explanations and walkthroughs

How many LeetCode problems should you solve? #leetcode #techinterview #developer #softwareengineer

CrioDo
0:58304,599 views

Asked By Companies

8 companies
M
Microsoft
A
Apple
Y
Yandex
O
Oracle
A
Amazon
G
Google
B
Bloomberg
B
Bytedance

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Linked List CycleEasy
Linked List Cycle IIMedium
Intersection of Two Linked ListsEasy
Two Sum III - Data structure designEasy
More similar problems

Related Topics

Hash TableTwo PointersStringSliding Window

Problem Stats

Acceptance Rate46.5%
DifficultyMedium
Companies8

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Permutation in String asked in FAANG interviews?

Yes, variations of this problem are frequently asked in technical interviews at FAANG and other top tech companies. It tests understanding of sliding window patterns, hash tables, and efficient string processing.

Why is sliding window used in Permutation in String?

Sliding window helps efficiently examine all substrings of a fixed length without recomputing character counts from scratch. It updates the counts incrementally as characters enter and leave the window, improving performance.

What data structure is best for Permutation in String?

A frequency array or hash table is commonly used to store character counts. For lowercase English letters, a fixed array of size 26 is usually preferred because it offers constant space and faster access.

What is the optimal approach for Permutation in String?

The optimal approach uses a sliding window combined with a frequency counter for characters. By maintaining a window of the same length as the first string and updating counts as the window moves, we can efficiently detect if a permutation exists.

=
strlen
(
s1
)
,
len2
=
strlen
(
s2
)
;
6
if
(
len1
>
len2
)
return
false
;
7
int
count1
[
26
]
=
{
0
}
,
count2
[
26
]
=
{
0
}
;
8
9
for
(
int
i
=
0
;
i
<
len1
;
++
i
)
{
10
count1
[
s1
[
i
]
-
'a'
]
++
;
11
count2
[
s2
[
i
]
-
'a'
]
++
;
12
}
13
14
for
(
int
i
=
len1
;
i
<
len2
;
++
i
)
{
15
if
(
memcmp
(
count1
,
count2
,
26
*
sizeof
(
int
)
)
==
0
)
return
true
;
16
count2
[
s2
[
i
]
-
'a'
]
++
;
17
count2
[
s2
[
i
-
len1
]
-
'a'
]
--
;
18
}
19
return
memcmp
(
count1
,
count2
,
26
*
sizeof
(
int
)
)
==
0
;
20
}
class
Solution
:
4
def
checkInclusion
(
self
,
s1
:
str
,
s2
:
str
)
-
>
bool
:
5
if
len
(
s1
)
>
len
(
s2
)
:
6
return
False
7
count1
=
defaultdict
(
int
)
8
count2
=
defaultdict
(
int
)
9
for
ch
in
s1
:
10
count1
[
ch
]
+=
1
11
for
ch
in
s2
[
:
len
(
s1
)
]
:
12
count2
[
ch
]
+=
1
13
matches
=
0
14
for
key
in
count1
:
15
if
count1
[
key
]
==
count2
[
key
]
:
16
matches
+=
1
17
l
=
0
18
for
r
in
range
(
len
(
s1
)
,
len
(
s2
)
)
:
19
if
matches
==
len
(
count1
)
:
20
return
True
21
new_char
=
s2
[
r
]
22
count2
[
new_char
]
+=
1
23
if
count2
[
new_char
]
==
count1
[
new_char
]
:
24
matches
+=
1
25
elif
count2
[
new_char
]
==
count1
[
new_char
]
+
1
:
26
matches
-=
1
27
28
old_char
=
s2
[
l
]
29
count2
[
old_char
]
-=
1
30
if
count2
[
old_char
]
==
count1
[
old_char
]
:
31
matches
+=
1
32
elif
count2
[
old_char
]
==
count1
[
old_char
]
-
1
:
33
matches
-=
1
34
l
+=
1
35
return
matches
==
len
(
count1
)

Explanation

This Python solution optimizes character comparison by leveraging a hash map to keep track of character frequency changes as the sliding window moves. Instead of full map comparison for each slide, it increments and decrements values while checking conditions, which reduces the overhead of comparing full data structures.