#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
Check Binary String for One Segment of Ones
More Relevant Posts
-
#day334 of #1001daysofcode problem statement (1009): Complement of Base 10 Integer 💡 Approach: To find the complement of a number, I converted it into binary and flipped every bit (0 → 1 and 1 → 0). After flipping the bits, the resulting binary string was converted back to decimal. Example: 5 → Binary: 101 Complement: 010 → 2 ⏱ Time Complexity: O(log n) 🧠 Space Complexity: O(log n) #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
Day 60 - Remove Duplicates from Sorted List Worked on removing duplicate elements from a sorted linked list. Approach: • Traverse the list once • Compare current node with next node • Skip duplicate nodes by adjusting pointers Time Complexity: O(n) Space Complexity: O(1) #Day60 #LeetCode #Java #LinkedList #CodingPractice #DSA #TechJourney
To view or add a comment, sign in
-
-
Day 59 - Merge Two Sorted Lists Worked on merging two sorted linked lists into one sorted list. Approach: • Used a dummy node to simplify pointer handling • Compared nodes from both lists and attached the smaller one • Appended remaining elements after traversal A classic linked list problem that strengthens pointer manipulation skills. Time Complexity: O(n + m) Space Complexity: O(1) #Day59 #LeetCode #Java #LinkedList #CodingPractice #DSA #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 38 of #100DaysOfCode 🌱 Topic: Linked List / Recursion ✅ Problem Solved: LeetCode 24 – Swap Nodes in Pairs 🛠 Approach: Used a recursive approach to swap every two adjacent nodes in the linked list. If the list has 0 or 1 node, return it directly (base case). Store the second node (head.next) as a temporary node. Recursively swap the remaining list starting from temp.next. Adjust pointers so the second node becomes the new head of the pair. Connect the swapped pair with the recursively processed list. This swaps nodes without modifying their values, only changing pointers. #100DaysOfCode #Day38 #DSA #LinkedList #Recursion #LeetCode #Java #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
#day332 of #1001daysofcode problem statement (0226): Invert Binary Tree The idea is to recursively swap the left and right child of every node in the tree. ⏱ Time Complexity: O(n) 🧠 Space Complexity: O(h) — recursion stack (h = height of tree) #1001DaysOfCode #DSA #Java #LeetCode #ProblemSolving Shivam Mahajan #leetcode
To view or add a comment, sign in
-
-
Day 62 — LeetCode Progress (Java) Problem: Merge Strings Alternately Required: Given two strings word1 and word2, merge them by alternating characters, starting from word1. If one string is longer, append the remaining characters at the end. Idea: Use two pointers to traverse both strings simultaneously and build the result step by step. Approach: Initialize two pointers for both strings. Iterate while either string still has characters: Pick one character from word1 (if available) Then pick one from word2 (if available) Continue alternating until both strings are fully traversed. Time Complexity: O(n + m) Space Complexity: O(n + m) #LeetCode #DSA #Java #Strings #TwoPointers #Algorithms #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 42 of #100DaysOfCode 🌱 Topic: Linked List / Two Pointers ✅ Problem Solved: LeetCode 82 – Remove Duplicates from Sorted List II 🛠 Approach: Used a dummy node to simplify handling edge cases where the head might be removed. When two consecutive nodes had the same value, stored that value. Skipped all nodes with that duplicate value using a loop. Linked the previous node to the next distinct node. Continued traversal until reaching the end. This ensures that only unique elements remain in the sorted list. #100DaysOfCode #Day42 #DSA #LinkedList #TwoPointers #LeetCode #Java #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 66 — LeetCode Progress (Java) Problem: Find the Difference of Two Arrays Required: Given two integer arrays, return: Elements present in nums1 but not in nums2 Elements present in nums2 but not in nums1 Idea: Use sets to remove duplicates and quickly check membership. Approach: Convert both arrays into sets Iterate over nums1 set: Add elements not present in nums2 set to result1 Iterate over nums2 set: Add elements not present in nums1 set to result2 Return both lists Time Complexity: O(n + m) Space Complexity: O(n + m) #LeetCode #DSA #Java #HashSet #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 15/100 – LeetCode Challenge Problem: Reverse Linked List Today’s problem focused on reversing a singly linked list using an iterative approach. Approach: Used three pointers to reverse the links: prev → keeps track of the previous node curr → current node being processed next → stores the next node before changing the link Steps: Store curr.next in next Reverse the link → curr.next = prev Move prev and curr one step forward Continue until the list ends. Finally, prev becomes the new head of the reversed list. Complexity: Time: O(n) Space: O(1) Concepts Practiced: Linked List pointer manipulation Iterative reversal technique In-place algorithm #100DaysOfCode #LeetCode #DSA #Java #LinkedList #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 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
-
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