🔥Day 91 of #100DaysOfLeetCode Problem: 1653. Minimum Deletions to Make String Balanced Difficulty: Medium Key Insight: A balanced string must have all 'a's before all 'b's. Any 'b' before an 'a' creates a violation. Approach: Preprocess the string using: - prefixB[i]: number of 'b's strictly before index i - suffixA[i]: number of 'a's strictly after index i For every index i, treat it as a split point and compute: deletions = prefixB[i] + suffixA[i] The minimum over all splits is the answer. Time Complexity: O(n) Space Complexity: O(n) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
Minimum Deletions to Balance String with LeetCode Solution
More Relevant Posts
-
🚀 DSA Consistency – Day 54 Solved “Check if Binary String Has at Most One Segment of Ones” on LeetCode. The task is to determine whether a binary string contains at most one contiguous block of 1s. Example: 1100111 → ❌ False (two segments of 1s) 111000 → ✅ True (only one segment) 🔹 Approach 1: Traverse the String Idea: While traversing the string, once we encounter a 0, no 1 should appear afterward. If 1 appears again, it means a second segment of ones has started. 🔹 Approach 2: String Pattern Check A binary string with only one segment of 1s should never contain "01" followed by another 1. So, we simply check for the pattern "01". Both the approaches have same complexities: Time Complexity: O(n) Space Complexity: O(1) #DSA #LeetCode #Java #ProblemSolving #CodingJourney #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 24 of #100DaysOfCode Solved 167. Two Sum II – Input Array Is Sorted on LeetCode 🔢👉👈 🧠 Key insight: Because the array is already sorted, we can avoid hash maps and use a two-pointer approach to find the target sum efficiently. ⚙️ Approach: 🔹Initialize two pointers at the start and end of the array 🔹Calculate the sum of values at both pointers 🔹If the sum is too large → move the right pointer left 🔹If the sum is too small → move the left pointer right 🔹Stop when the target is found ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #TwoPointers #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
#day329 of #1001daysofcode problem statement (1784): Check if Binary String Has at Most One Segment of Ones Approach 1: If the pattern "01" appears in the string, it means the sequence of '1's was broken Approach 2: Traverse the string and count segments of consecutive '1's. Whenever a '1' is found, skip the entire sequence of continuous '1's and increase the segment count. If the number of segments of '1's is exactly one, the condition is satisfied. --both approaches have same time and space complexity. ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(1) #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
🚀 Day 99 of #100DaysOfLeetCode 📌 Problem: 67. Add Binary 📊 Difficulty: Easy 💡 Key Insight: Binary addition follows the same rules as decimal addition — add digits from right to left while maintaining a carry. The only difference is everything is in base 2. 🛠️ Approach: Use two pointers starting from the end of both strings Add corresponding bits along with a carry Append the result bit (sum % 2) Update carry (sum / 2) Reverse the final string to get the answer ⏱️ Complexity: Time: O(n) Space: O(n) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
📘 LeetCode Daily — Balance a Binary Search Tree Solved this by first using inorder traversal to convert the BST into a sorted list, then reconstructing the tree from the middle element to ensure balance. This approach clearly showed how traversal order captures structure and how choosing the midpoint at each step naturally leads to a height-balanced tree. A neat example of combining traversal + divide-and-conquer. #LeetCode #BinarySearchTree #InorderTraversal #DivideAndConquer #DSA #Java
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 11 of #100DaysOfCode Solved Find Minimum in Rotated Sorted Array on LeetCode using Binary Search 🔄 🧠 Key insight: In a rotated sorted array, at least one half is always sorted. By comparing the middle element with the right boundary, we can determine which half contains the minimum and safely discard the other. ⚙️ Approach: 🔹Use binary search on the array 🔹Compare nums[mid] with nums[right] 🔹If nums[mid] > nums[right] → minimum lies in the right half 🔹Else → minimum lies in the left half (including mid) ⏱️ Time Complexity: O(log n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 13 of #100DaysOfCode Solved Remove Linked List Elements on LeetCode 🔗 🧠 Key insight: While traversing a linked list, careful pointer updates are needed—especially when the head node itself matches the value to be removed. ⚙️ Approach: 🔹Handle cases where the head contains the target value 🔹Traverse the list using a pointer 🔹Skip nodes whose value matches the given target by adjusting next pointers ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #LinkedList #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
Day 29/100 – LeetCode Challenge 🚀 Problem: Same Tree Approach: Used recursive traversal Compared corresponding nodes of both trees Checked: both nodes null → identical one null → not identical values mismatch → not identical recursively compared left and right subtrees Time Complexity: O(n) Space Complexity: O(h) Key takeaway: Many tree comparison problems reduce to simultaneous traversal of both trees. #LeetCode #100DaysOfCode #DSA #Java #BinaryTree #ProblemSolving
To view or add a comment, sign in
-
-
Day 23 - Find Minimum in Rotated Sorted Array Technique Used: Modified Binary Search Key Idea: Since the array is rotated but originally sorted, used binary search to identify the pivot (minimum element). Compared mid with end to determine which half is unsorted and adjusted boundaries accordingly. Time Complexity: O(log n) #Day23 #LeetCode #Java #BinarySearch #DSA #Algorithms
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