How to approach strinf window and subarray problem

https://online.codingblocks.com/app/player/42387/content/21161/5389 and
https://online.codingblocks.com/app/player/42387/content/21161/5173

Hi @chemant077

Q1. Subarray with distinct elements:
Approach : If we know all elements in a subarray arr[i…j] are distinct, sum of all lengths of distinct element subarrays in this sub array is ((j-i+1)*(j-i+2))/2. ( All subsets have all elements distinct )

We first find largest subarray (with distinct elements) starting from first element. We count sum of lengths in this subarray using above formula. For finding next subarray of the distinct element, we increment starting point, i and ending point, j unless (i+1, j) are distinct. If not possible, then we increment i again and move forward the same way.

Q2. String Window
Approach: Start matching the two strings from the start and maintain a hashmap to store which elements have been found. Once the frequency of all characters of string2 is atleast 1 ( ie all characters have been found) try to minimize the window by popping elements from the start of the window which are not required( frequency > 1)
When there is no extra element at the start of window, start adding elements to the window and repeat the above step.

I hope this helps you solve both the questions :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.