Permutation in String Leetcode Solution

Permutation in String Leetcode Problem :

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.

 

jump game leetcode

Permutation in String Leetcode Solution :

Constraints :

  • 1 <= s1.length, s2.length <= 10^4
  • s1 and s2 consist of lowercase English letters.

Example 1:

  • Input: s1 = “ab”, s2 = “eidboaoo”
  • Output: false

Approach :
The alphabets in a permutation that is to be searched will always be the same, and so will their count.
The thing that counts in s2 is that each substring has the same number of characters as in s1. As a result, we make a hashmap that contains the count of each character in the string s1. After that, we slide a window over the string s2 and lower the number for characters that appear in the window. When all of the counters in the hashmap reach 0, we’ve found the permutation.

 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Code :

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription