Skip to content

567. Permutation in String #667

Answered by kovatz
mah-shamim asked this question in Q&A
Oct 5, 2024 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

We can use the sliding window technique with a frequency counter, rather than trying every possible substring permutation (which would be too slow for large inputs).

Key Idea:

  1. If a permutation of s1 is a substring of s2, both strings must have the same character frequencies for the letters present in s1.
  2. We can use a sliding window of length s1 over s2 to check if the substring within the window is a permutation of s1.

Steps:

  • Create a frequency counter for s1 (i.e., count the occurrences of each character).
  • Slide a window over s2 of size equal to s1 and check if the frequency of characters in the window matches the frequency of characters in s1.
  • If the frequencies match, it means that …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Oct 5, 2024
Maintainer Author

@kovatz
Comment options

kovatz Oct 5, 2024
Collaborator

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty hacktoberfest hacktoberfest hacktoberfest-accepted hacktoberfest accepted
2 participants