Check if a Binary String Contains Only One Segment of 1s Problem: Given a binary string s, determine whether the string contains only one contiguous segment of '1's. If we ever find a '1' that appears after a '0', it means a new segment of 1s has started — which violates the condition. Approach: -Iterate through the string. -Check if the previous character is '0' and the current character is '1'. -If that happens, it means there are multiple segments of 1s, so return false. 🔍 Time Complexity:O(n) 🔍 Space Complexity:O(1) #javascript #typescript #algorithms #datastructures #leetcode #codingpractice #frontenddeveloper #softwareengineering #problemSolving #100DaysOfCode
Checking Binary String for Single Segment of 1s
More Relevant Posts
-
📌 #68 DailyLeetCodeDose Today's problem: 86. Partition List – 🟡 Medium The goal is to rearrange a linked list so that all nodes with values less than x come before nodes greater than or equal to x, while preserving their original relative order and a clean way to solve this problem is to build two lists: 1-st one for nodes < x 2-nd for nodes >= x Then simply connect them together – this keeps the original order and runs in O(n) time with O(1) extra space. https://lnkd.in/e3Sa4AWC #DailyLeetCodeDose #LeetCode #JavaScript #Algorithms #ProblemSolving #Coding
To view or add a comment, sign in
-
-
📌 #75 DailyLeetCodeDose Today's problem: 108. Convert Sorted Array to Binary Search Tree – 🟢 Easy This is a classic divide and conquer problem – we pick the middle element as the root, then recursively construct the left subtree from the left half of the array and the right subtree from the right half. Easy :) https://lnkd.in/eyecRNEj #DailyLeetCodeDose #LeetCode #JavaScript #Algorithms #ProblemSolving #Coding
To view or add a comment, sign in
-
-
📌 #67 DailyLeetCodeDose Today's problem: 61. Rotate List – 🟡 Medium Before rotating the list, we should determine its length. If the length is 3 and the number of rotations is 10, it's safe to say that we only need to perform one rotation instead of 10 to get the same result (10 % 3 === 1). The second important idea is that we can perform all the rotations at once — we just need to identify the new head and the new tail, connect the old tail to the head, and then break the list at the correct position. That's essentially what my code does. https://lnkd.in/eEnjh6ZF #DailyLeetCodeDose #LeetCode #JavaScript #Algorithms #ProblemSolving #Coding
To view or add a comment, sign in
-
-
📌 #71 DailyLeetCodeDose Today's problem: 69. Sqrt(x) – 🟢 Easy Finding the integer square root of a number – meaning the largest integer whose square is less than or equal to the given number – can be done in a very straightforward way. You could simply start from 0, keep adding 1, and check if the square exceeds the number. This works perfectly for small numbers and is easy to understand. However, there's a way to improve efficiency by using 𝐛𝐢𝐧𝐚𝐫𝐲 𝐬𝐞𝐚𝐫𝐜𝐡. Instead of checking every number one by one, binary search allows us to quickly narrow down the range and find the result much faster, especially for large numbers. https://lnkd.in/eURbjQ8g #DailyLeetCodeDose #LeetCode #JavaScript #Algorithms #ProblemSolving #Coding
To view or add a comment, sign in
-
-
📌 #66 DailyLeetCodeDose Today's problem: 82. Remove Duplicates from Sorted List II – 🟡 Medium Sure, you could solve it with two passes, like I initially did: 1. Traverse the linked list and collect all values. 2. Traverse it again and remove all nodes with non-unique values. 💂 But there is an important detail in the constraints that allows for a much more efficient solution: the list is already sorted – so instead of using two passes with extra storage, you can keep track of the previous node and check whether the current node has the same value as the next one. If it does, skip all nodes with that value in one go. Easy :) https://lnkd.in/eJQVrSY7 #DailyLeetCodeDose #LeetCode #JavaScript #Algorithms #ProblemSolving #Coding
To view or add a comment, sign in
-
-
Solved another classic Linked List problem today: Swap Nodes in Pairs on LeetCode. Approach: Used a recursive strategy to swap nodes in pairs. Base condition: if the list has 0 or 1 node, return it as is. For each pair: Store the second node. Recursively process the remaining list. Reverse the current pair by updating pointers. Key pointer operations: temp = head.next head.next = swapPairs(head.next.next) temp.next = head Result: Runtime: 0 ms (Beats 100%) Memory: 52.66 MB (Beats 97.59%) Problems like this reinforce how powerful recursion and pointer manipulation are when working with linked lists. #DSA #LinkedList #LeetCode #JavaScript #ProblemSolving #DataStructures #Algorithms #CodingPractice #SoftwareEngineering #BackendDevelopment
To view or add a comment, sign in
-
-
Problem: Palindrome Linked List ✔️ All test cases passed ✔️ Runtime: 141 ms My Approach (Simple & Intuitive): Instead of modifying the linked list or using complex pointer tricks, I went with a straightforward idea: 👉 Traverse the linked list once 👉 Build two strings: 1. One in forward order 2. One in reverse order 👉 Compare both strings If they match → it's a palindrome ✅ Yes, it's not the most optimal in terms of space, but it gets the job done cleanly. Complexity: Time: O(n) Space: O(n) #LeetCode #DSA #LinkedList #Algorithms #JavaScript #ProblemSolving #CodingJourney #LearnInPublic
To view or add a comment, sign in
-
-
Just solved the Binary Search problem in JavaScript. Binary Search is a great example of the Divide and Conquer approach. Instead of checking every element, the algorithm looks at the middle of a sorted array and eliminates half of the search space each step. Because the array is divided in half every time, the time complexity becomes O(log n), which makes it much faster than linear search O(n) for large datasets. Small problem, but a great reminder of how powerful algorithmic thinking can be. #JavaScript #Algorithms #BinarySearch #CodingPractice #LeetCode #SoftwareEngineering
To view or add a comment, sign in
-
Implemented the Bubble Sort algorithm in JavaScript to sort an array in ascending order using a simple comparison and swapping technique. Demonstrates a fundamental sorting approach with O(n²) time complexity, ideal for understanding core algorithm concepts. #JavaScript #DSA #Algorithms #Coding #WebDevelopment
To view or add a comment, sign in
-
-
📌 #72 DailyLeetCodeDose Today's problem: 50. Pow(x, n) – 🟡 Medium Multiplying a number by itself in a loop would technically work, but because the exponent range can be huge (-2^31 <= n <= 2^31 - 1), such a solution would take far too long. Instead, we can reduce the complexity to O(log n) using 𝐄𝐱𝐩𝐨𝐧𝐞𝐧𝐭𝐢𝐚𝐭𝐢𝐨𝐧 𝐛𝐲 𝐒𝐪𝐮𝐚𝐫𝐢𝐧𝐠. The idea is simple: instead of multiplying x by itself n times, we repeatedly square the base and halve the exponent. This works because any power can be represented in binary form. Each step processes one bit of the exponent, which reduces the total number of operations from O(n) to O(log n). ... Here is a boar, btw: 🐗 :D https://lnkd.in/e4ZREVwC #DailyLeetCodeDose #LeetCode #JavaScript #Algorithms #ProblemSolving #Coding
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