Given a string s and a pattern p, find all starting positions in s where p (or its rearrangement) occurs.
Explanation: Step 1: Create a hashmap to store the frequency of characters in the pattern. The hashmap will be {'g': 1, 'c': 1, 'a': 1}. Step 2: Create a sliding window of size equal to the length of the pattern. Iterate over the string and for each window, calculate the frequency of characters in the window. Step 3: If the frequency of characters in the window is equal to the frequency of characters in the pattern, add the starting position of the window to the result list. Step 4: Return the result list.
Explanation: Step 1: Create a hashmap to store the frequency of characters in the pattern. The hashmap will be {'a': 1, 'b': 1}. Step 2: Create a sliding window of size equal to the length of the pattern. Iterate over the string and for each window, calculate the frequency of characters in the window. Step 3: If the frequency of characters in the window is equal to the frequency of characters in the pattern, add the starting position of the window to the result list. Step 4: Return the result list.
Analyze constraints and compute optimal solutions step-by-step.
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak