Leetcode 567: Permutation in String

grid47
grid47
Exploring patterns and algorithms
Sep 11, 2024 5 min read

A string where permutations are checked, each valid permutation softly glowing as it is found.
Solution to LeetCode 567: Permutation in String Problem

Given two strings s1 and s2, return true if s2 contains any permutation of s1 as a substring, otherwise return false. In other words, check if one of the permutations of s1 exists as a substring within s2.
Problem
Approach
Steps
Complexity
Input: The input consists of two strings, s1 and s2.
Example: Input: s1 = "abc", s2 = "eidbacooo"
Constraints:
• 1 <= s1.length, s2.length <= 10^4
• s1 and s2 consist of lowercase English letters.
Output: The output should be a boolean value: true if a permutation of s1 exists as a substring in s2, false otherwise.
Example: Output: true
Constraints:
• The result should be a boolean value.
Goal: The goal is to check if any permutation of string s1 is a substring of s2.
Steps:
• Create frequency count arrays for s1 and s2.
• Iterate through s2, adjusting the frequency array of the current window of size equal to the length of s1.
• If the frequency arrays of s1 and the current window of s2 match, return true.
• If no such match is found after checking all windows of s2, return false.
Goal: The problem constraints ensure that both s1 and s2 are non-empty and consist of lowercase English letters.
Steps:
• 1 <= s1.length, s2.length <= 10^4
• s1 and s2 consist of lowercase English letters.
Assumptions:
• The input strings s1 and s2 will only contain lowercase letters.
• The solution should handle edge cases like when s1 is larger than s2.
Input: Input: s1 = "abc", s2 = "eidbacooo"
Explanation: The string s2 contains a permutation of s1, such as "cba" within the substring "eidbacooo".

Input: Input: s1 = "abc", s2 = "eidboaoo"
Explanation: There is no permutation of s1 in s2, hence the result is false.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus