🚀 Day 45 / 100 | Longest Substring Without Repeating Characters Intuition: The problem is to find the length of the longest substring without repeating characters. If a repeated character appears, the substring must be adjusted to remove duplicates. Using a sliding window approach, it maintain a window of unique characters while scanning the string. Approach: O(n) -Use a HashSet to store characters currently in the substring window. -Maintain two pointers: left and right . -Iterate through the string using the right pointer. -If the character already exists in the set, remove characters from the left until the duplicate is removed. -Add the current character to the set. -Update the maximum length of the substring using the window size. -Repeat this process until the end of the string. Complexity: Time Complexity: O(n) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode #SlidingWindow
Longest Substring Without Repeating Characters in Java
More Relevant Posts
-
✨ Day 38 of 90 – Pattern Mastery Journey 🧠 Pattern:Binary Triangle Pattern 💡 Approach: ✔ Used nested loops to control rows and columns ✔ Applied a simple condition `(i + j) % 2` to alternate values ✔ Printed ‘1’ when the sum is even, otherwise ‘0’ ✔ No extra variables needed — clean and efficient logic 🚀 This problem helped me understand how **mathematical conditions can simplify pattern logic**, making the code more optimized and readable. #PatternMasteryJourney #Java #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 39 of #100DaysOfCode Solved 80. Remove Duplicates from Sorted Array II on LeetCode 🔢 🧠 Key Insight: The array is already sorted, and we need to ensure that each element appears at most twice, modifying the array in-place. ⚙️ Approach: 🔹Maintain a pointer i representing the position to place the next valid element 🔹Start iterating from index 2 🔹For each element nums[j], compare it with nums[i] 🔹If they are different, place the element at nums[i + 2] and move the pointer forward This ensures that no element appears more than twice while maintaining the sorted order. ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Arrays #TwoPointers #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
Day 91 of #365DaysOfLeetCode Challenge Today’s problem: **Arithmetic Slices (LeetCode 413)** An interesting problem that focuses on identifying patterns in subarrays. The goal is to count all contiguous subarrays of length ≥ 3 where the difference between consecutive elements remains constant. 💡 **Key Insight:** Instead of checking every subarray (which would be inefficient), we track the current streak of arithmetic sequences. * If the current 3 elements form an arithmetic sequence → extend the streak * Keep adding the count of valid slices ending at current index * Reset when the pattern breaks 📌 **Approach:** * Use two variables: * `curr` → counts current valid extensions * `total` → accumulates final answer * Traverse from index 2 onward * Compare consecutive differences ⚡ **Time Complexity:** O(n) ⚡ **Space Complexity:** O(1) **What I learned today:** Sometimes, problems that look like they need nested loops can be optimized using pattern tracking and dynamic accumulation. Consistency is key — 91 days down, 274 to go! #LeetCode #DSA #CodingChallenge #Java #ProblemSolving #100DaysOfCode #TechJourney
To view or add a comment, sign in
-
-
Day 16/100 – LeetCode Challenge Problem: Merge Sorted Array Today’s problem involved merging two sorted arrays into one sorted array. Approach: Created a temporary array of size m + n Used two pointers to compare elements from both arrays Inserted the smaller element into the new array Copied remaining elements if any array still had values Finally copied the merged result back into nums1 Complexity: Time: O(m + n) Space: O(m + n) Concepts Practiced: Two-pointer technique Array traversal Merging sorted arrays #100DaysOfCode #LeetCode #DSA #Java #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟕𝟔/𝟑𝟔𝟓 🚀 📌 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐎𝐓𝐃: 𝐃𝐞𝐜𝐨𝐝𝐞 𝐭𝐡𝐞 𝐒𝐥𝐚𝐧𝐭𝐞𝐝 𝐂𝐢𝐩𝐡𝐞𝐫𝐭𝐞𝐱𝐭 Continuing my 𝟑𝟔𝟓 𝐃𝐚𝐲𝐬 𝐨𝐟 𝐂𝐨𝐝𝐞 journey with a focus on 𝐩𝐫𝐨𝐛𝐥𝐞𝐦-𝐬𝐨𝐥𝐯𝐢𝐧𝐠, 𝐃𝐒𝐀, 𝐚𝐧𝐝 𝐜𝐨𝐧𝐬𝐢𝐬𝐭𝐞𝐧𝐜𝐲. 💪 🔎 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Treat the encoded string as a matrix with given rows. Traverse diagonally starting from each column of the first row. Append characters while moving diagonally (down-right). Trim trailing spaces from the result. 🔍 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦 𝐮𝐬𝐞𝐝: Matrix simulation with diagonal traversal. ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) 🧠 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: 𝐎(𝐧) 📈 𝐊𝐞𝐲 𝐭𝐚𝐤𝐞𝐚𝐰𝐚𝐲: Many string problems can be visualized as matrix traversals — changing perspective simplifies the solution. #LeetCode #LeetCodeDaily #365DaysOfCode #DSA #Java #Strings #Matrix #Simulation #ProblemSolving #LearningInPublic 👨💻 🔗 Problem link in comments 👇
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 87/100 – 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐂𝐡𝐚𝐥𝐥𝐞𝐧𝐠𝐞 🚀 Problem: 228. 𝐒𝐮𝐦𝐦𝐚𝐫𝐲 𝐑𝐚𝐧𝐠𝐞𝐬 Today I solved a problem where we need to summarize consecutive numbers in a sorted unique array into ranges. 🔑 𝐈𝐝𝐞𝐚: Traverse the array and keep extending the range while consecutive numbers continue. Once the sequence breaks, close the range and store it. 💡 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Start with the first element as start Move forward while nums[i] + 1 == nums[i+1] If range exists → "start->end" Else → single number "start" 𝐊𝐞𝐲 𝐈𝐧𝐬𝐢𝐠𝐡𝐭: Efficient single pass solution (O(n)) by grouping consecutive elements on the fly. #LeetCode #Java #ProblemSolving #DSA #100DaysOfCode #CodingJourney
To view or add a comment, sign in
-
-
100 Days of Code Day-14 Solved a classic problem: Longest Common Prefix Given an array of strings, the task is to find the longest common prefix shared among them. If no common prefix exists, return an empty string. Approach: Used a simple horizontal scanning method: Start with the first string as the prefix Compare it with each string Gradually reduce the prefix until it matches all strings Example: "flower", "flow", "flight" → "fl" "dog", "racecar", "car" → "" A good exercise to strengthen string handling and logical thinking. #Java #DSA #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 50 / 100 | Median of Two Sorted Arrays Intuition: We are given two sorted arrays and need to find the median of the combined numbers. Since both arrays are already sorted, we can merge them in sorted order. Once we have the merged array, finding the median becomes simple. If the total number of elements is odd, the median is the middle element. If it's even, the median is the average of the two middle elements. Approach: Use two pointers to traverse both arrays. Compare the elements and insert the smaller one into a new array. Continue this process until all elements are merged. Finally, calculate the median based on the length of the merged array. Complexity: Time Complexity: O(n + m) Space Complexity: O(n + m) #100DaysOfCode #Java #DSA #LeetCode #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 40 of #100DaysOfCode Solved 378. Kth Smallest Element in a Sorted Matrix on LeetCode 🔍📊 🧠 Key Insight: Each row and column in the matrix is sorted, so instead of flattening and sorting the matrix, we can apply Binary Search on the value range. ⚙️ Approach: 1️⃣ Define the search range: 🔹left = smallest element (matrix[0][0]) 🔹right = largest element (matrix[n-1][n-1]) 2️⃣ Perform binary search on values: 🔹Pick mid 🔹Count how many elements in the matrix are ≤ mid 3️⃣ If count ≥ k → move left side (potential answer found) 4️⃣ Else → move right side This efficiently narrows down the kth smallest element. ⏱️ Time Complexity: O(n log(max-min)) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Matrix #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 6 – #50daysLeetCodeChallenge Today I solved the Remove Duplicates from Sorted Array problem. 📌 Problem Statement Given a sorted array, remove the duplicates in-place such that each unique element appears only once. Return the number of unique elements k, and ensure the first k elements contain the final result. Example: Input: [0,0,1,1,1,2,2,3,3,4] Output: k = 5 → [0,1,2,3,4,...] 💡 Approach I Used – Two Pointer Technique Since the array is already sorted, duplicates will always be adjacent. 1️⃣ Use two pointers: j → tracks the position of the last unique element i → scans through the array 2️⃣ Compare nums[i] with nums[j] 3️⃣ If they are different: Move j forward Place the new unique element at nums[j] #LeetCode #Java #DSA #CodingChallenge #ProblemSolving #SoftwareEngineering
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