🚀 DSA Series – Problem Solving Today I solved a classic String problem: Removing Duplicate Characters. 🧩 Problem Statement Given a string, remove duplicate characters while maintaining the order of first occurrence. Example: Input: `"programming"` Output: `"progamin"` 🧠 Optimized Approach (Using Boolean Array) Instead of using a HashSet, I used a boolean array to track characters. This works great when the character set is limited (like ASCII). 🔹 Step-by-step Logic 1️⃣ Create a boolean array of size 256 (for all ASCII characters). * Each index represents a character. * Default value is `false` → means character not seen yet. 2️⃣ Create a StringBuilder to store the result. 3️⃣ Traverse the string character by character: * For each character `ch`, check `if (seen[ch] == false)` → Mark it as seen: `seen[ch] = true` → Append it to StringBuilder * If already `true`, skip it (duplicate). 4️⃣ Convert StringBuilder to string — final answer ready ✅ ⏱ Time & Space Complexity Time Complexity: O(n) We traverse the string only once. Space Complexity:O(1) Because the boolean array size is fixed (256), not dependent on input size. 💡 Key Learning Choosing the right data structure matters a lot. Boolean arrays can be faster and more memory-efficient than HashSet when the character range is small. Small optimizations = Big performance impact 🚀 #DSA #Java #ProblemSolving #CodingJourney #Placements #LearningInPublic
Removing Duplicate Characters in Strings with Boolean Array
More Relevant Posts
-
⚡ 𝗗𝗮𝘆 𝟳𝟱 𝗼𝗳 𝗠𝘆 𝟭𝟬𝟬 𝗗𝗮𝘆𝘀 𝗼𝗳 𝗗𝗦𝗔 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲! 𝘛𝘰𝘥𝘢𝘺’𝘴 𝘱𝘳𝘰𝘣𝘭𝘦𝘮 𝘸𝘢𝘴 𝘢 𝘨𝘳𝘦𝘢𝘵 𝘮𝘪𝘹 𝘰𝘧 𝙨𝙩𝙧𝙞𝙣𝙜 𝙥𝙧𝙤𝙘𝙚𝙨𝙨𝙞𝙣𝙜 𝙖𝙣𝙙 𝙢𝙖𝙩𝙝𝙚𝙢𝙖𝙩𝙞𝙘𝙖𝙡 𝙩𝙝𝙞𝙣𝙠𝙞𝙣𝙜, 𝘪𝘯𝘴𝘱𝘪𝘳𝘦𝘥 𝘣𝘺 𝘢 𝘳𝘦𝘢𝘭-𝘸𝘰𝘳𝘭𝘥 𝘴𝘺𝘴𝘵𝘦𝘮 𝘸𝘦 𝘢𝘭𝘭 𝘳𝘦𝘤𝘰𝘨𝘯𝘪𝘻𝘦 𝘌𝘹𝘤𝘦𝘭 𝘤𝘰𝘭𝘶𝘮𝘯𝘴. 📌 Problem Solved: 1️⃣ 𝗟𝗲𝗲𝘁𝗰𝗼𝗱𝗲 171: Excel Sheet Column Number ✨ Key Learnings: 🔹 This problem is essentially about 𝗯𝗮𝘀𝗲-𝟮𝟲 𝗻𝘂𝗺𝗯𝗲𝗿 𝗰𝗼𝗻𝘃𝗲𝗿𝘀𝗶𝗼𝗻, where characters act as digits instead of numbers. 🔹 Iterating through the string and updating the result as result = result * 26 + currentCharValue helps build the column number efficiently. 🔹 It reinforced how 𝗰𝗵𝗮𝗿𝗮𝗰𝘁𝗲𝗿-𝘁𝗼-𝗻𝘂𝗺𝗯𝗲𝗿 𝗺𝗮𝗽𝗽𝗶𝗻𝗴 ('A' → 1 to 'Z' → 26) plays a crucial role in many string-based problems. 🧠 Big Takeaway: Problems that look string-heavy often boil down to 𝘀𝗶𝗺𝗽𝗹𝗲 𝗺𝗮𝘁𝗵 + 𝗶𝘁𝗲𝗿𝗮𝘁𝗶𝗼𝗻 once the pattern is clear. Day 75 completed — slowly but surely stacking fundamentals! 💪🔥 #100DaysOfCode #DSA #Java #Strings #ProblemSolving #LogicBuilding #InterviewPreparation #LeetCode #LearningInPublic #Developers
To view or add a comment, sign in
-
-
Day 3 – DSA Journey | Arrays 🚀 Today’s focus was on 3-sum variations, where sorting + two pointers do most of the heavy lifting. ✅ Problems Solved 📌 3Sum 📌 3Sum Closest 🔹 3Sum Approach: Sorted the array Fixed one element and used two pointers to find valid triplets Skipped duplicates to avoid repeated results Key Learning: ✅ Sorting simplifies multi-pointer logic ✅ Duplicate handling is critical for correctness Complexity: ⏱ Time: O(n²) 📦 Space: O(1) (excluding output) 🔹 3Sum Closest Approach: Sorted the array Fixed one element and adjusted pointers based on how close the sum was to the target Updated the closest sum whenever a better match was found Key Learning: ✅ Greedy updates help converge quickly ✅ Absolute difference comparison is a powerful pattern Complexity: ⏱ Time: O(n²) 📦 Space: O(1) 🧠 Takeaway Once you understand sorting + two pointers, many array problems start looking familiar. Consistency > Complexity. On to Day 4 🔁🚀 #DSA #Arrays #TwoPointers #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
🚀 DSA Daily Challenge – Day 7 🧠 Problem: Remove Element (LeetCode 27) 💡 Problem Statement: Given an integer array nums and an integer val, remove all occurrences of val in-place and return the number of remaining elements. ⚠️ You must modify the array in-place with O(1) extra space. 🔥 Intuition Brute force idea: Create a new array and copy elements ≠ val ❌ But the problem requires in-place modification. So how do we solve it efficiently? 👉 Use the Two Pointer Technique 🛠 Approach (Two Pointers – In Place) • Use one pointer i to traverse the array • Use another pointer index to track where the next valid element should go • If nums[i] != val → copy it to nums[index] • Increment index At the end, index represents the new length. ⚙️ Complexity ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) 👉 Think Two Pointers + Overwriting Pattern This pattern is very common in coding interviews 🔥 #LeetCode #LeetCode27 #RemoveElement #DSA #DataStructures #Algorithms #CodingInterview #InterviewPreparation #Java #TwoPointers #ArrayProblems #InPlaceAlgorithm #TimeComplexity #BigO #CompetitiveProgramming #FAANGPrep #100DaysOfCode
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟱𝟲/𝟭𝟬𝟬 — 𝗕𝗮𝗰𝗸 𝘁𝗼 𝘁𝗵𝗲 𝗖𝗹𝗮𝘀𝘀𝗶𝗰𝘀 Day 56. Valid Parentheses. The problem everyone sees on Day 1 of learning stacks. Except this time? I actually get why it works. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟮𝟬: Valid Parentheses (Easy) 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Given a string of brackets: (), {}, []. Check if they're properly matched and nested. Examples: "()" → Valid "([)]" → Invalid (wrong order) "{[]}" → Valid (properly nested) 𝗧𝗵𝗲 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: Stack. That's it. Push opening brackets. When you see a closing bracket, check if it matches the stack top. If yes, pop. If no, invalid. Empty stack at the end = valid. First time I saw this problem, I thought "why use a stack?" Now I see it—LIFO matches the nesting structure perfectly. 𝗪𝗵𝘆 𝗜𝘁 𝗠𝗮𝘁𝘁𝗲𝗿𝘀: This isn't just about parentheses. It's about recognizing when a problem needs LIFO behavior. Compilers use this. Code editors use this. Expression parsing uses this. Pattern recognition >> memorization. 𝗖𝗼𝗱𝗲: https://lnkd.in/gdCu84Ja 56 down. 44 to go. 𝗗𝗮𝘆 𝟱𝟲/𝟭𝟬𝟬 ✅ #100DaysOfCode #LeetCode #Stack #DataStructures #Algorithms #ProblemSolving #CodingInterview #Programming #Java #PatternRecognition
To view or add a comment, sign in
-
𝐃𝐚𝐲 𝟐𝟒 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on applying binary search in a 2D matrix by treating it like a flattened sorted array. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Search a 2D Matrix 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 𝟏 – 𝐓𝐨𝐩-𝐑𝐢𝐠𝐡𝐭 𝐒𝐜𝐚𝐧 • Start from the top-right corner • Move left if the current value is greater • Move down if the current value is smaller • Time Complexity: O(n + m) 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 𝟐 – 𝐑𝐨𝐰-𝐖𝐢𝐬𝐞 𝐁𝐢𝐧𝐚𝐫𝐲 𝐒𝐞𝐚𝐫𝐜𝐡 • Identify the possible row • Apply binary search inside that row • Time Complexity: O(n log m) 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 𝟑 – 𝐅𝐥𝐚𝐭𝐭𝐞𝐧𝐞𝐝 𝐁𝐢𝐧𝐚𝐫𝐲 𝐒𝐞𝐚𝐫𝐜𝐡 • Treat matrix as a 1D sorted array • Range: 0 to (n × m − 1) • Convert index → row = mid / m, col = mid % m • Apply standard binary search 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • A 2D matrix can be treated as 1D with index mapping • Binary search is about reducing the search space logically • Index math simplifies multidimensional problems • Choosing the right abstraction makes problems easier 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(log(n × m)) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Sometimes the trick is not searching in 2D — it’s thinking in 1D. 24 days consistent. On to Day 25 🚀 #DSA #Arrays #BinarySearch #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟒 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problems pushed me to think more about optimization, pruning, and duplicate handling. ✅ 𝐏𝐫𝐨𝐛𝐥𝐞𝐦𝐬 𝐒𝐨𝐥𝐯𝐞𝐝 📌 4Sum 📌 Remove Duplicates from Sorted Array 🔹 𝟒𝐒𝐮𝐦 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Sorted the array Fixed two indices and used 𝐭𝐰𝐨 𝐩𝐨𝐢𝐧𝐭𝐞𝐫𝐬 for the remaining pair Applied 𝐞𝐚𝐫𝐥𝐲 𝐩𝐫𝐮𝐧𝐢𝐧𝐠 using the minimum and maximum possible sums Carefully skipped duplicates at every level 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: ✅ Pruning reduces unnecessary iterations ✅ Using long avoids integer overflow ✅ Duplicate handling is the hardest (and most important) part 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: ⏱ Time: O(n³) 📦 Space: O(1) (excluding output) 🔹 𝐑𝐞𝐦𝐨𝐯𝐞 𝐃𝐮𝐩𝐥𝐢𝐜𝐚𝐭𝐞𝐬 𝐟𝐫𝐨𝐦 𝐒𝐨𝐫𝐭𝐞𝐝 𝐀𝐫𝐫𝐚𝐲 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Used 𝐭𝐰𝐨 𝐩𝐨𝐢𝐧𝐭𝐞𝐫𝐬 One pointer tracks the last unique element Overwrote duplicates in-place 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: ✅ Sorted arrays enable in-place solutions ✅ Simple logic can be extremely efficient 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: ⏱ Time: O(n) 📦 Space: O(1) 🧠 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Optimization is not about writing complex code — it’s about 𝐚𝐯𝐨𝐢𝐝𝐢𝐧𝐠 𝐮𝐧𝐧𝐞𝐜𝐞𝐬𝐬𝐚𝐫𝐲 𝐰𝐨𝐫𝐤. On to 𝐃𝐚𝐲 𝟓 🔁🚀 #DSA #Arrays #TwoPointers #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
Finished the core IDE practice for Multidimensional Arrays today. The focus here was on searching inside sorted matrices and understanding how the approach changes performance, not just correctness. I worked on: - basic search through full traversal - optimized search starting from a matrix corner - handling matrix updates like Set Matrix Zeroes using better space strategies These problems made it clear that matrix questions are often about choosing the right starting point, not just writing more loops. int[][] matrix = { {1, 4, 7, 11}, {2, 5, 8, 12}, {3, 6, 9, 16}, {10, 13, 14, 17} }; int target = 9; int i = 0; int j = matrix[0].length - 1; boolean found = false; // Efficient search from top-right corner while (i < matrix.length && j >= 0) { if (matrix[i][j] == target) { found = true; break; } else if (matrix[i][j] > target) { j--; } else { i++; } } System.out.println(found); // Output: true What became clearer from this stage: - better starting position can reduce time complexity - optimization is often about direction, not new logic - matrix problems reward thinking before coding - many advanced questions reuse the same core ideas This felt like a good checkpoint before moving to LeetCode matrix problems. #Java #DSA #Matrices #Arrays #LearningInPublic #ProblemSolving #CodingJourney #Programming #JavaDeveloper
To view or add a comment, sign in
-
📅 Day 36 out of 100 — Solving LeetCode Problems Daily, Kickstarting My Java + DSA Journey 🚀 📘 Course: Data Structures & Algorithms 📈 One Problem a Day: Consistency Compounding. Leetcode Problem_189. Rotate Array Given an integer array nums, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] Example 2: Input: nums = [-1,-100,3,99], k = 2 Output: [3,99,-1,-100] Explanation: rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] Constraints: 1 <= nums.length <= 105 -231 <= nums[i] <= 231 - 1 Every question teaches me something new — whether it’s logic building, pattern recognition, or writing cleaner code. Solved today’s problem focusing on optimizing time complexity and handling edge cases carefully. #LeetCode #DailyCoding #DSA #ProblemSolving #100DaysOfCode #CodingJourney #LeetCode #Consistency #TechGrowth #CodeEveryday #LeetCode #GrowthMindset
To view or add a comment, sign in
-
-
📌 17. Letter Combinations of a Phone Number (Medium) Today’s problem was all about Backtracking & Recursion. 🧠 Problem Summary Given digits from 2–9, return all possible letter combinations based on phone keypad mapping. Example: Input: "23" Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"] 💡 Key Insight This is a classic Cartesian Product problem. Each digit expands into multiple choices → Perfect use case for Backtracking. 🚀 Approach Create a digit → letter mapping Use recursion to build combinations When current string length == digits length → Add to result Backtrack and explore next possibilities ⏱ Complexity Time: O(4ⁿ) Space: O(n) recursion stack Max combinations = 256 (since n ≤ 4) 🎯 What I Practiced Today ✅ Recursion Tree Thinking ✅ Backtracking Pattern ✅ StringBuilder Optimization ✅ Clean Code Structure Consistency > Motivation 💪 Day 18 completed. #LeetCode #Java #DataStructures #Algorithms #Backtracking #100DaysOfCode
To view or add a comment, sign in
-
-
Day 9 – DSA Journey | Arrays 🚀 Today’s problem helped me understand backtracking with re-use of elements 🔁 and how recursion explores all valid combinations. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • ➕ Combination Sum 🔹 Combination Sum 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • 🔁 Used backtracking to build combinations • 🎯 Reduced the remaining target at each step • ♻️ Reused the same element by passing the current index again • 📌 Stored a valid path only when the remaining target becomes zero 𝐇𝐨𝐰 𝐢𝐭 𝐖𝐨𝐫𝐤𝐬 • ➡️ Start from a given index to avoid duplicate combinations • ➕ Add the current number to the path • 🔽 Recurse with reduced remaining sum • ⏪ Backtrack by removing the last added number 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • 🧠 Backtracking explores all valid paths systematically • ♻️ Passing the same index allows unlimited reuse of elements • 🎯 Base conditions control recursion flow • 🧹 Clean backtracking keeps the solution correct 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • ⏱ Time: Exponential (depends on number of combinations) • 📦 Space: O(target) (recursion depth + path) 🧠 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Backtracking isn’t about speed — It’s about exploring all possibilities correctly 🔍 On to Day 10 🔁🚀 #DSA #Arrays #Backtracking #Recursion #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
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