Solving Permutation in String Problem with Brute Force and Optimized Approaches

DevLog Day 44 Today I solved one of the trickiest problems I have come across so far. My brain is still hurting but here we are. The problem was Permutation in String. You are given two strings s1 and s2 and you need to check if any permutation of s1 exists as a substring inside s2. Simple to read, painful to solve. I worked through both a brute force and an optimized approach. The brute force sorts every window of s2 and compares it against the sorted s1, which works but costs O(n²) time. The optimized approach swaps sorting for a hashmap that tracks letter frequencies across a sliding window, which is faster in practice but honestly just as tricky to wrap your head around when you are seeing it for the first time. The best solution goes even further with a pure sliding window that avoids nested loops entirely. I will be honest, I do not fully understand the logic behind it yet and I know I will need to revisit this one a few times. But that is fine. Some problems are not meant to click on the first try and I know this one will teach me something deeper about sliding windows once it does. #100DaysOfCode #LeetCode #NeetCode #DataStructures #SlidingWindow #CodingJourney #ProblemSolving #Programming #SoftwareEngineering #Tech

To view or add a comment, sign in

Explore content categories