Day 14/30 – LeetCode #3 (Longest Substring Without Repeating Characters) | Java A brute-force approach would check all possible substrings, which quickly becomes inefficient. The challenge here was maintaining a substring with unique characters while scanning the string only once. Using a sliding window with a HashSet, I expanded the window when characters were unique and shrank it whenever a duplicate appeared. This ensured each character was processed at most twice, resulting in an O(n) solution. This problem clearly demonstrated how sliding window techniques help manage dynamic constraints efficiently. #LeetCode #Java #DSA #SlidingWindow #HashSet #Strings #Consistency #LearningInPublic
Java Solution: Longest Substring Without Repeating Characters
More Relevant Posts
-
Title: Longest Substring with K Distinct Characters (Sliding Window) Solved the problem “Longest Substring with K Distinct Characters” using an optimized Sliding Window + HashMap approach in Java. 🔹 What the solution does: - Expands the window using a right pointer while tracking character frequency in a HashMap. - Shrinks the window from the left when the number of distinct characters exceeds k. - Continuously updates the maximum length when exactly k distinct characters are present. - Runs in O(n) time complexity, making it efficient for large inputs. 🔹 Example: Input: "s = "aababcbebebe", k = 3" Output: 7 This problem strengthened my understanding of sliding window techniques, hashing, and efficient substring optimization in DSA. #DSA #Java #SlidingWindow #HashMap #ProblemSolving
To view or add a comment, sign in
-
-
📝 Day 15/30 – LeetCode #209 (Minimum Size Subarray Sum) | Java The brute-force approach checks all possible subarrays, which leads to O(n²) time complexity. The key observation here is that all numbers are positive, allowing us to use a sliding window efficiently. By expanding the window to reach the target sum and shrinking it to find the minimum length, each element is visited at most twice. This results in an O(n) time solution with constant space. This problem strengthened my understanding of when sliding window techniques are applicable and why input constraints matter. #LeetCode #Java #DSA #SlidingWindow #Arrays #ProblemSolving #Consistency #LearningInPublic
To view or add a comment, sign in
-
-
📝 Day 16/30 – LeetCode #15 (3Sum) | Java This problem extends the two-sum pattern and introduces duplicate handling as the main challenge. A brute-force approach would require checking all triplets, resulting in O(n³) time complexity. By sorting the array and fixing one element, I applied a two-pointer approach on the remaining part of the array. Careful duplicate skipping ensured unique triplets while maintaining efficiency. This optimized the solution to O(n²) time. This problem reinforced how sorting combined with pointer logic can drastically reduce complexity. #LeetCode #Java #DSA #TwoPointers #Arrays #ProblemSolving #Consistency #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 76 of #100DaysOfCode Today I solved a string manipulation problem: Clear Digits 🧠 Problem: Given a string, whenever a digit appears, remove the previous character. Digits act like a backspace operation. 💡 Key Insight: Instead of modifying the string directly (since Strings are immutable in Java), I used a StringBuilder, which works like a stack: If character → append (push) If digit → delete last character (pop) 📌 What I Learned: StringBuilder is powerful for string modifications Always handle edge cases (like digit at the start) Many string problems are actually stack problems in disguise ⏱ Complexity: Time: O(n) Space: O(n) #Java #DSA #LeetCode #CodingJourney #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
Pattern Sliding Window : Maximum Sum of K-Size Subarray (Sliding Window Technique) Problem Solved Implemented an efficient solution to find the maximum sum of any contiguous subarray of size ‘K’ using the Sliding Window approach in Java. 🔹 What this solution does: - Computes the sum of the first window of size K - Slides the window one element at a time - Adds the new element and removes the previous one - Keeps track of the maximum sum found so far 🔹 Example: Input: arr = [100, 200, 300, 400], k = 2 Output: 700 This problem helped strengthen my understanding of window-based optimization and time complexity improvement from O(n²) to O(n). #DSA #Java #SlidingWindow #ProblemSolving #CodingPractice #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 18/180 | #180DaysOfCode 📍 LeetCode | 💻 Java Solved: 1528. Shuffle String Used a direct indexing approach by placing each character at its correct position using the given indices array. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(n) A simple yet important problem to strengthen understanding of arrays and index mapping. 💪 Consistency continues 🚀 #DSA #LeetCode #Java #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day: 43/365 📌 LeetCode POTD: Binary Number with Alternating Bits Easy Key takeaways/Learnings from this problem: 1. This one shows how clean bit tricks can replace messy string checks when working with binary patterns. 2. Big takeaway: sometimes a small observation (like using XOR or shifting) turns a pattern problem into a super neat one-liner. #POTD #365DaysOfCode #DSA #Java #ProblemSolving #LearningInPublic #Consistency 🥷
To view or add a comment, sign in
-
-
Day 5 | Problem 1 – Score of a String Today I practised calculating the score of a string by finding the absolute difference between adjacent characters. Approach used: • Traverse the string from left to right • Calculate the absolute ASCII difference between consecutive characters • Add each difference to a running sum • Return the final score This problem helped me better understand character handling in strings, ASCII values, and loop-based traversal. #Java #DSA #StringProblems #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
Title: Longest Substring Without Repeating Characters (Sliding Window) Solved LeetCode Problem 3 – Longest Substring Without Repeating Characters using an optimized Sliding Window + HashSet approach in Java. 🔹 What the solution does: - Expands the window using a right pointer to explore characters. - Uses a HashSet to track unique characters in the current window. - Shrinks the window from the left whenever a duplicate character is found. - Continuously updates the maximum length of a valid substring. - Achieves an efficient O(n) time complexity. 🔹 Example: Input: "s = "abcabcbb"" Output: 3 (""abc"") This problem strengthened my understanding of two-pointer techniques, sliding window optimization, and efficient substring handling in DSA. #DSA #Java #SlidingWindow #HashSet #ProblemSolving #LeetCode3
To view or add a comment, sign in
-
-
🚀 Day 31 of Importance of Static block, Static variables and Static methods Today, I explored one of the most important concepts in Java – Static Keyword 🔥 🔹 Static Variables Belong to the class, not to objects. Only one copy is created and shared among all instances. Memory is allocated only once when the class is loaded. Accessed using the class name. 🔹 Static Methods Belong to the class rather than an object. Can be called without creating an object. Mostly used for utility methods. Cannot directly access non-static instance variables. 🔹 Static Block Executes when the class is loaded into memory. Used to initialize static variables. Runs before constructors. Executes only once. Static members are shared resources of a class. They improve memory efficiency and are perfect for common data or utility logic. Understanding how static works also helped me visualize JVM memory concepts like Class Area, Stack, and Heap. #Day31 #Java #LearningJourney #StaticKeyword #Programming #BackendDevelopment
To view or add a comment, sign in
-
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