🚀 𝗗𝗦𝗔 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴 𝗝𝗼𝘂𝗿𝗻𝗲𝘆 – 𝗦𝗼𝗹𝘃𝗶𝗻𝗴 𝗟𝗼𝗻𝗴𝗲𝘀𝘁 𝗦𝘂𝗯𝘀𝘁𝗿𝗶𝗻𝗴 𝗪𝗶𝘁𝗵𝗼𝘂𝘁 𝗥𝗲𝗽𝗲𝗮𝘁𝗶𝗻𝗴 𝗖𝗵𝗮𝗿𝗮𝗰𝘁𝗲𝗿𝘀 Most people jump straight to the answer… But the real skill is learning how to optimize step by step. 🧩 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 👉 Given a string, find the length of the longest substring without repeating characters. Example: "abcabcbb" → Output: 3 ("abc") 🔗 𝗚𝗶𝘁𝗛𝘂𝗯 𝗖𝗼𝗱𝗲: https://lnkd.in/gTmWkphw 🔴 𝟭. 𝗕𝗿𝘂𝘁𝗲 𝗙𝗼𝗿𝗰𝗲 (O(n³)) Try all possible substrings Check if each substring has unique characters Keep track of max length ❌ Works, but very slow 🟡 𝟮. 𝗕𝗲𝘁𝘁𝗲𝗿 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 (O(n²)) Start from each index Use a set to track characters Stop when duplicate appears ✅ Faster, but still not optimal 🟢 𝟯. 𝗢𝗽𝘁𝗶𝗺𝗮𝗹 – 𝗦𝗹𝗶𝗱𝗶𝗻𝗴 𝗪𝗶𝗻𝗱𝗼𝘄 (O(n)) 💡 𝗜𝗱𝗲𝗮: Don’t restart… adjust the window! Use two pointers (left & right) Expand window (right++) If duplicate → shrink (left++) Track max length 🔥 𝗗𝗿𝘆 𝗥𝘂𝗻 (Simple View) String: "abcabcbb" Step-by-step: a → ab → abc ✅ (max = 3) duplicate found → shrink window continue expanding… 💡 𝗞𝗲𝘆 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴 Instead of solving again and again, 👉 reuse previous work using a sliding window. 📈 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆 Understand → Improve → Optimize This mindset is what clears interviews 🚀 💻 𝗪𝗵𝗲𝗿𝗲 𝗶𝘁 𝗶𝘀 𝘂𝘀𝗲𝗱 ✔ Substring problems ✔ Longest/shortest window problems ✔ Real-time data stream problems #DSA #Coding #Python #SlidingWindow #ProblemSolving #100DaysOfCode #InterviewPrep #LearningJourney
Optimizing Substring Problems with Sliding Window Technique
More Relevant Posts
-
🔥 DSA Series | Day 11 We all have seen the classic Two Sum / Pair Sum problem everywhere — even trending on LinkedIn. But today, a small twist struck my mind… 🤔 👉 What if we need to find ALL UNIQUE pairs that sum to a target? 💡 Problem Input: arr = [1,1,2,2,3,3] target = 4 👉 Expected Output: [[1,3], [2,2]] 🚀 Approach (Two Pointers + Sorting) Instead of brute force ❌ (O(n²)), we: Sort the array Use two pointers (i, j) Move intelligently based on sum Skip duplicates to ensure uniqueness 🧠 Code class Solution: def uniquePairs(self, arr: list[int], target: int) -> list[list[int]]: arr.sort() n = len(arr) i = 0 j = n - 1 res = [] while i < j: total = arr[i] + arr[j] if total == target: res.append([arr[i], arr[j]]) i += 1 j -= 1 # 🔁 Skip duplicates while i < j and arr[i] == arr[i - 1]: i += 1 while i < j and arr[j] == arr[j + 1]: j -= 1 elif total < target: i += 1 else: j -= 1 return res if res else [[-1, -1]] ⚡ Complexity ⏱️ Time → O(n log n) (sorting dominates) 📦 Space → O(1) (excluding output) 🔥 Key Insight 💡 Removing duplicates smartly during traversal is more efficient than removing them beforehand. 🧠 Takeaway Sometimes a small twist in a common problem → turns it into a great interview question What would you choose? 🤔 🔹 Hashing (O(n)) 🔹 Two Pointers (O(n log n), cleaner) Let’s discuss 👇 #DSA #Coding #Python #InterviewPrep #ProblemSolving #100DaysOfCode #Tech #Learning
To view or add a comment, sign in
-
-
🚀 Day 1 — Building Problem Solving Skills I’m continuing my journey to improve problem-solving skills by staying consistent, disciplined, and accountable — one problem at a time. 🧩 Problem: • Two Sum (LeetCode #1) 📚 Topic: Arrays (Pair Traversal) 💡 Key Insight: Checking all possible pairs works, but it highlights the need for more efficient approaches as input size grows. ⚡ Approach: • Pick an element using index i • Traverse remaining elements using index j • Check if nums[i] + nums[j] == target • Return indices when condition is satisfied 🎯 Takeaway: Even simple problems help strengthen logic and introduce optimization thinking. ⏱ Complexity: Time → O(n²) Space → O(1) 💻 Sample Input: nums = [2, 7, 11, 15] target = 9 ✅ Output: [0, 1] Consistency > Perfection 💪 #DSA #LeetCode #ProblemSolving #Python #CodingJourney #LearningInPublic #Arrays #TechGrowth
To view or add a comment, sign in
-
-
🚀 Cracked “Top K Frequent Elements” with an Optimal Approach! Today I solved one of the most important interview problems on LeetCode using an efficient Bucket Sort approach (O(n)) — and got it accepted ✅ 🔍 Problem Insight: Instead of sorting (which takes O(n log n)), I used frequency as an index to directly access elements with higher occurrence. 💡 Key Learnings: How to reduce time complexity from O(n log n) → O(n) Using hashmaps (Counter) for frequency counting Applying bucket sort for optimization Writing clean and interview-ready code ⚡ Complexity Analysis: Time Complexity: O(n) (Frequency count + bucket fill + traversal) Space Complexity: O(n) (Hashmap + bucket storage) ⚡ Performance: Runtime: 10 ms 🧠 Approach Summary: Count frequency of elements Store elements in buckets based on frequency Traverse from highest frequency to get top K elements 📌 Consistency > Perfection Every problem solved is one step closer to mastering DSA. #DataStructures #Algorithms #Python #LeetCode #CodingJourney #ProblemSolving #TechGrowth #Consistency #Learning #DSA
To view or add a comment, sign in
-
-
Hello dudes and dudettes!! 🚀 Day 21/150 — Solved LeetCode 151: Reverse Words in a String Today’s problem was a great reminder that even something as simple as a sentence can hide a few tricky details 😄 At first glance, reversing words sounds easy… But once spaces start behaving weirdly, things get interesting. 🧠 What’s the Problem About? You’re given a string that contains words separated by spaces. Your task is to: 👉 Reverse the order of words 👉 Not the characters inside them 💡 Example Input: "the sky is blue" Output: 👉 "blue is sky the" 🔥 The Twist It’s not just about reversing words… The string might contain: Extra spaces at the beginning Extra spaces at the end Multiple spaces between words Example: " hello world " Expected Output: 👉 "world hello" So we also need to clean up the spaces while solving it. 🧠 The Thought Process Instead of overcomplicating things, I broke it down: Extract the words (ignore spaces) Reverse their order Join them back with a single space That’s it. Simple steps → clean solution. ⚙️ Why This Approach Works Splitting automatically removes extra spaces Reversing handles the order Joining ensures proper formatting No unnecessary checks. No messy conditions. 😎 Why This Problem Is Interesting It looks easy but tests your handling of edge cases Shows how built-in operations can simplify problems Reinforces the idea that clarity beats complexity 💡 What I Learned Clean input → clean output Breaking a problem into steps makes it easier Sometimes the best solution is the simplest one 🎯 Key Takeaway “Don’t fight the problem — break it down and let simple steps solve it.” 🔥 Another step forward in the journey — writing cleaner, smarter solutions. #LeetCode #Algorithms #Strings #ProblemSolving #CodingJourney #100DaysOfCode #Python #LearningInPublic
To view or add a comment, sign in
-
-
Yesterday I got the API working and posted from a Python script. Today I'm building the landing page. Here's what StreakPosts actually is: You sign up. Every morning you get a question by email or text. You reply in plain English. AI turns your answer into a polished social post and schedules it automatically. No dashboard. No blank page. No excuses. The goal is simple — show up consistently for 90 days and watch what happens to your audience. Anyone can do it. Most people just need a system. That's what I'm building. Show up raw. Every day. 🔥 #buildinpublic #StreakPosts #indiehacker
To view or add a comment, sign in
-
🚀 Day 3 – Building a Problem-Solving Mindset Still on the grind. Still showing up. Because consistency > motivation. 📌 Today’s Problem: Sum of N Natural Numbers Looks simple. But again… the goal is not just solving — it’s how efficiently you think. 🔹 Approaches Explored 1️⃣ Naive Approach → Basic step-by-step addition 2️⃣ Using Recursion → Function calling itself to solve smaller parts 3️⃣ Formula-Based Approach → Direct mathematical solution for maximum efficiency 💡 Key Takeaway Same problem. Three different approaches. Different levels of thinking. ✔️ Learned how multiple solutions exist for one problem ✔️ Understood that efficiency improves with better thinking 📈 Progress Update: From solving problems → to choosing the best approach Consistency is the real unlock 🔑 #Python #ProblemSolving #100DaysOfCode #Consistency #LearningJourney #DeveloperMindset
To view or add a comment, sign in
-
-
🚀 Day 18 of #DSAStreak 📌 Problem: 3Sum 🧠 Problem Summary: Given an integer array nums, find all unique triplets [nums[i], nums[j], nums[k]] such that: 👉 i ≠ j ≠ k 👉 nums[i] + nums[j] + nums[k] == 0 👉 No duplicate triplets allowed 💡 Approach: Sorting + Two Pointer 🔥 Instead of using brute force (O(n³) ❌), we optimize using: ✔ Sort the array ✔ Fix one element (i) ✔ Use two pointers (left, right) to find remaining two numbers ⚙ Key Logic: 👉 If sum == 0 → store triplet 👉 If sum < 0 → move left pointer 👉 If sum > 0 → move right pointer 👉 Skip duplicates to avoid repeated answers 💻 Code: class Solution(object): def threeSum(self, nums): nums.sort() result = [] for i in range(len(nums)): if i > 0 and nums[i] == nums[i - 1]: continue left, right = i + 1, len(nums) - 1 while left < right: total = nums[i] + nums[left] + nums[right] if total == 0: result.append([nums[i], nums[left], nums[right]]) left += 1 right -= 1 while left < right and nums[left] == nums[left - 1]: left += 1 while left < right and nums[right] == nums[right + 1]: right -= 1 elif total < 0: left += 1 else: right -= 1 return result ⏱ Time Complexity: O(n²) 💾 Space Complexity: O(1) (excluding output) 🚀 Key Takeaways: ✔ Sorting enables two-pointer optimization ✔ Duplicate handling is crucial ✔ Classic interview problem — must master! #DSA #LeetCode #Python #Coding #InterviewPrep #100DaysOfCode
To view or add a comment, sign in
-
I was cleaning a dataset — filtering rows, transforming values, the usual. My 5-line for loop worked fine. But I wanted to be "Pythonic." So I compressed it into a one-liner. Then I added another layer. The next morning I stared at it for two full minutes trying to decode my own logic. If I couldn't read it, my future teammates had no chance. This carousel breaks down: → The mental model that makes list comprehensions click instantly → The reading order most beginners get backwards → The exact rule for when to stop using them and write a real loop What's the longest you've stared at your own code before realizing you had no idea what it does? #Python #DataAnalytics #DataAnalyst #PythonTips #LearnInPublic #AHAMoments #DataAnalystJourney
To view or add a comment, sign in
-
We put the Hermes agent to work on cold email outreach to pull in new partnerships and sponsors for BridgeMind. After just eight minutes of processing a to-do list and delegating tasks, it was already executing code. It looks like it is using Python to run search requests entirely in the background. I am letting it do its thing right now. I will share the final results once the automated outreach is finished. Drop a comment if you want to see the final results of this automated outreach. #vibecoding #agenticcoding #aicoding #python #coldemail #aiagents #shipfast
To view or add a comment, sign in
-
We put the Hermes agent to work on cold email outreach to pull in new partnerships and sponsors for BridgeMind. After just eight minutes of processing a to-do list and delegating tasks, it was already executing code. It looks like it is using Python to run search requests entirely in the background. I am letting it do its thing right now. I will share the final results once the automated outreach is finished. Drop a comment if you want to see the final results of this automated outreach. #vibecoding #agenticcoding #aicoding #python #coldemail #aiagents #shipfast
To view or add a comment, sign in
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development