🚀Day 24 of #75DaysofLeetCode LeetCode Problem Solved: Removing Stars From a String (2390) Today I solved the problem “Removing Stars From a String.” 🔹 Problem Statement: We are given a string containing characters and *. Each * removes the closest non-star character to its left, along with the * itself. The task is to return the final string after all stars are processed. 💡 Approach: The most efficient way to solve this is using a Stack-like approach. 1️⃣ Traverse the string from left to right. 2️⃣ If the character is not *, add it to the result (stack). 3️⃣ If the character is *, remove the last added character. 4️⃣ Continue until the string is processed. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 💻 Java Implementation: class Solution { public String removeStars(String s) { StringBuilder sb = new StringBuilder(); for(char c : s.toCharArray()) { if(c == '*') { sb.deleteCharAt(sb.length() - 1); } else { sb.append(c); } } return sb.toString(); } } ✨ Key Learning: Using a stack or StringBuilder helps efficiently simulate the removal of the closest character on the left. #LeetCode #Java #DataStructures #ProblemSolving #CodingPractice #100DaysOfCode
Removing Stars From a String with Java
More Relevant Posts
-
🚀 Day 99 of My 100 Days LeetCode Challenge | Java Today’s problem was all about randomization and linked list traversal — a nice break from heavy DP and matrices. The challenge was to design a system that returns a random node’s value from a singly linked list, ensuring that every node has an equal probability of being chosen. Since linked lists don’t allow direct indexing, the key idea was to first determine the size of the list, and then generate a random index to fetch the corresponding node. This approach ensures uniform randomness while keeping the implementation simple and efficient. ✅ Problem Solved: Linked List Random Node ✔️ All test cases passed (8/8) ⏱️ Runtime: 11 ms 🧠 Approach: Linked List Traversal + Randomization 🧩 Key Learnings: ● Randomization problems require ensuring uniform probability distribution. ● Linked lists limit direct access, so traversal becomes essential. ● Precomputing size can simplify random selection. ● Sometimes simple approaches are the most effective. ● Understanding data structure limitations helps design better solutions. This problem highlighted how probability + data structures can come together in elegant ways. 🔥 Day 99 complete — sharpening my understanding of randomization and linked list behavior. #LeetCode #100DaysOfCode #Java #LinkedList #Randomization #Algorithms #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 15 of #50DaysLeetCode Challenge Today I solved the “Longest Common Prefix” problem on LeetCode using Java. 🔹 Problem: Find the longest common prefix among an array of strings. If no common prefix exists, return an empty string "". Example: Input: ["flower","flow","flight"] Output: "fl" Input: ["dog","racecar","car"] Output: "" 🔹 Approach I Used: ✔ Took the first string as the initial prefix ✔ Compared it with each string in the array ✔ If mismatch occurs, reduced the prefix step by step ✔ Continued until all strings share the same prefix 🔹 Key Insight: You don’t need complex logic — just keep shrinking the prefix until it matches all strings. 🔹 Concepts Practiced: • String manipulation • Iterative comparison • Edge case handling #LeetCode #DSA #Java #Algorithms #ProblemSolving #CodingChallenge #50DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 97 of My 100 Days LeetCode Challenge | Java Today’s problem was a solid exercise in matrix processing and prefix sum optimization. The goal was to count the number of submatrices whose sum is less than or equal to a given value (k). A brute-force approach would be too slow, so the key was to use 2D prefix sums to efficiently compute submatrix sums. By converting the matrix into a prefix sum matrix, we can calculate the sum of any submatrix in constant time, making the overall solution much more efficient. ✅ Problem Solved: Count Submatrices With Sum ≤ K ✔️ All test cases passed (859/859) ⏱️ Runtime: 7 ms 🧠 Approach: 2D Prefix Sum 🧩 Key Learnings: ● Prefix sums are powerful for optimizing repeated range sum queries. ● 2D prefix sums extend the same idea from arrays to matrices. ● Preprocessing can drastically reduce computation time. ● Avoiding brute force is key in large input problems. ● Matrix problems often become easier with the right transformation. This problem reinforced how preprocessing techniques like prefix sums can turn complex problems into efficient solutions. 🔥 Day 97 complete — sharpening matrix optimization and prefix sum skills. #LeetCode #100DaysOfCode #Java #PrefixSum #Matrix #Algorithms #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
📌 LeetCode Daily Challenge — Day 29 Problem: 3442. Make Two Strings Equal by Swapping Topic: String, Hash Table 📌 Quick Problem Sense: Two strings of length 4. You can swap characters at indices i and j only if j - i = 2. That means only swaps (0↔2) and (1↔3) are allowed, on either string, any number of times. Can you make both strings equal? 🤔 🧠 Approach (Simple Thinking): 🔹 Ask the key question first: which positions can actually interact? 🔹 Swap (0↔2) and (0↔1)? NEVER, gap must be exactly 2 🔹 So even positions {0, 2} are forever isolated from odd positions {1, 3} 🔹 Within each group of 2, you can freely swap, so any arrangement within the group is reachable 🔹 The strings are equalizable if and only if: {s1[0], s1[2]} and {s2[0], s2[2]} contain the same characters (even group) ✅ {s1[1], s1[3]} and {s2[1], s2[3]} contain the same characters (odd group) ✅ 🔹 Just sort each pair and compare, done! 🎯 ⏱️ Time Complexity: Fixed length 4 → everything is constant → O(1) 📦 Space Complexity: Four char arrays of size 2 → O(1) I wrote a full breakdown with dry run, real-life analogy, and step-by-step code walkthrough here 👇 https://lnkd.in/g_ExG2Xd If you solved it with a frequency map or a different parity grouping trick, drop it in the comments, always curious to see how others think about it 💬 See you in the next problem 👋 #LeetCode #DSA #CodingChallenge #Java #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 11 of #50DaysLeetCode Challenge Today I tackled a HARD problem: “Regular Expression Matching” on LeetCode using Java. 🔹 Problem: Given a string "s" and a pattern "p", implement regex matching with support for: • "." → matches any single character • "*" → matches zero or more of the preceding element Return true if the entire string matches the pattern. Example: Input: "s = "aa", p = "a*"" → Output: "true" Input: "s = "aa", p = "a"" → Output: "false" 🔹 Approach I Used: I used Dynamic Programming (DP) — anything else here is inefficient or breaks on edge cases. ✔ Created a 2D DP table "dp[m+1][n+1]" ✔ "dp[i][j]" represents whether "s[0...i-1]" matches "p[0...j-1]" ✔ Handled "*" carefully: - Treat it as zero occurrence → "dp[i][j-2]" - Or one/more occurrence → match previous character ✔ Built the solution bottom-up 🔹 Concepts Practiced: • Dynamic Programming (2D DP) • String pattern matching • Handling complex edge cases #LeetCode #DSA #Java #DynamicProgramming #Algorithms #CodingChallenge #50DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 536 of #750DaysOfCode 🚀 Today I solved Count Submatrices With Equal Frequency of X and Y (LeetCode 3212) using Java. 🔹 Problem Summary: Given a grid containing 'X', 'Y', and '.', we need to count the number of submatrices starting from the top-left corner (0,0) such that: • The number of 'X' and 'Y' is equal • The submatrix contains at least one 'X' 🔹 Approach: Instead of checking every possible submatrix (which would be too slow), I used the Prefix Sum technique. I maintained two prefix matrices to store the count of 'X' and 'Y' up to each cell. For every position (i, j), I checked: countX == countY and countX > 0 If true, that submatrix is valid. This reduces the complexity to O(n × m), which works efficiently for large grids. 🔹 Key Concepts Learned: ✅ Prefix Sum in 2D ✅ Matrix traversal optimization ✅ Handling constraints up to 1000 × 1000 ✅ Clean implementation in Java Consistent practice is making problem-solving faster and more structured every day. #750DaysOfCode #Day536 #LeetCode #Java #DataStructures #Algorithms #PrefixSum #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟵𝟰/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟭𝟱𝟯𝟵. 𝗞𝘁𝗵 𝗠𝗶𝘀𝘀𝗶𝗻𝗴 𝗣𝗼𝘀𝗶𝘁𝗶𝘃𝗲 𝗡𝘂𝗺𝗯𝗲𝗿 | 🟢 Easy | Java Marked as Easy — but the optimal solution is pure binary search brilliance. 🎯 🔍 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 Given a sorted array, find the kth missing positive integer. Linear scan works — but can we do O(log n)? 💡 𝗧𝗵𝗲 𝗞𝗲𝘆 𝗜𝗻𝘀𝗶𝗴𝗵𝘁 At index i, the value arr[i] should be i+1 in a complete sequence. So missing numbers before arr[i] = arr[i] - 1 - i This lets us binary search on the count of missing numbers! ⚡ 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵 ✅ If arr[mid] - 1 - mid < k → not enough missing numbers yet, go right ✅ Else → too many missing, go left ✅ After the loop, left + k gives the exact answer 𝗪𝗵𝘆 𝗹𝗲𝗳𝘁 + 𝗸? After binary search, left is the index where the kth missing number falls beyond. left numbers exist in the array before that point, so the answer is left + k. No extra passes needed. ✨ 📊 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆 ⏱ Time: O(log n) — vs O(n) linear scan 📦 Space: O(1) This is a perfect example of binary searching on a derived condition, not just a value. A real upgrade from the naive approach. 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gVYcjNS6 𝟲 𝗺𝗼𝗿𝗲 𝗱𝗮𝘆𝘀. 𝗧𝗵𝗲 𝗳𝗶𝗻𝗶𝘀𝗵 𝗹𝗶𝗻𝗲 𝗶𝘀 𝗿𝗶𝗴𝗵𝘁 𝘁𝗵𝗲𝗿𝗲! 🏁 #LeetCode #Day94of100 #100DaysOfCode #Java #DSA #BinarySearch #Arrays #CodingChallenge #Programming
To view or add a comment, sign in
-
💻 Day 9/100 – LeetCode Challenge Today I tackled LeetCode 344 – Reverse String (Easy). The task: Reverse a character array in-place, without using extra space. I practiced: ✅ Two-pointer technique ✅ Array manipulation ✅ In-place modifications Java Solution (Two-pointer method): class Solution { public void reverseString(char[] s) { int left = 0, right = s.length - 1; while(left < right){ char temp = s[left]; s[left] = s[right]; s[right] = temp; left++; right--; } } } Example: Input: ['h','e','l','l','o'] → Output: ['o','l','l','e','h'] Takeaways: Two pointers from ends → swap toward the center In-place reversal saves memory (O(1) extra space) Great foundation for problems like rotate array, palindrome checks, and reverse linked lists #100DaysOfCode #LeetCode #Java #CodingChallenge #TwoPointers #Day9
To view or add a comment, sign in
-
-
Day 35 of #75DaysOfLeetCode 🚀 LeetCode 1448 — Count Good Nodes in Binary Tree Just solved an interesting tree problem that really strengthens DFS concepts! 🌳 👉 Problem Insight: A node in a binary tree is considered “good” if no node on the path from root to it has a value greater than it. 💡 Approach: Traverse the tree using DFS Keep track of the maximum value seen so far If current node ≥ max_so_far → it's a good node Update max and continue traversal 🧠 This problem is a great example of: ✔️ Tree traversal (DFS) ✔️ Passing state in recursion ✔️ Decision making at each node ⏱ Complexity: Time: O(n) Space: O(h) 💻 Java Code: class Solution { public int goodNodes(TreeNode root) { return dfs(root, root.val); } private int dfs(TreeNode node, int maxSoFar) { if (node == null) return 0; int count = 0; if (node.val >= maxSoFar) { count = 1; maxSoFar = node.val; } count += dfs(node.left, maxSoFar); count += dfs(node.right, maxSoFar); return count; } } 🔥 Key Takeaway: Always think about what information needs to be carried along the recursion path — here, it's the maximum value so far. #LeetCode #DataStructures #BinaryTree #DFS #Java #CodingInterview #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 52/100 Today’s problem was based on String Manipulation — reversing the first k characters of a string. 🧠 What I learned: - How to efficiently manipulate strings - Using "StringBuilder" for reversal in Java - Writing clean and optimized code 💡 Approach: Reversed the first k characters and appended the remaining string. ⚡ Key Insight: Even simple problems help strengthen fundamentals, which are crucial for solving complex problems later. 👨💻 Code (Java): class Solution { public String reversePrefix(String s, int k) { String first = new StringBuilder(s.substring(0, k)).reverse().toString(); String rest = s.substring(k); return first + rest; } } 📈 Consistency is the key — showing up every day matters more than perfection. #Day52 #CodingChallenge #Java #DSA #LearningJourney #Consistency #100DaysOfCode
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