Implementing strStr() on LeetCode and HackerRank

🚀 Day 26 of #100DaysOfCode — LeetCode + HackerRank Edition! Today’s challenge was all about string precision and pattern matching: 🔗 Implement strStr() (LeetCode) 📌 Challenge: Given two strings haystack and needle, return the index of the first occurrence of needle in haystack, or -1 if it doesn’t exist. 🔍 Approach: → Skipped the built-in .find() to implement it manually → Iterated through haystack only up to len(haystack) - len(needle) → Compared slices haystack[i:i+len(needle)] with needle → Returned the index on match, -1 otherwise 💡 What made it click: → Realized that checking every possible starting index is enough — no need to build substrings manually → Adjusting the loop range to avoid out-of-bound errors was the key → Removing redundant checks like if needle not in haystack made the code cleaner and faster 📚 What I learned: ✅ Substring search is a great intro to sliding window logic ✅ Clean loop bounds = fewer bugs ✅ Sometimes the simplest approach is the most elegant ✅ Writing your own version of built-ins deepens your understanding of how they work under the hood Have you implemented your own strStr() before? Did you go brute-force or try KMP? Let’s compare strategies 💬🔍 #Day26 #LeetCode #StringMatching #CodingChallenge #Python #ProblemSolving #CleanCode #CodeEveryDay #LearnToCode #CodeNewbie #SoftwareEngineering #TechJourney #geeksforgeeks #100DaysOfCode

  • graphical user interface, text, application, chat or text message

To view or add a comment, sign in

Explore content categories