Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emilwijayasekara/leetcode-1784-check-if-binary-string-has-at-most-one-segment-of-ones
LeetCode Problem 1784. Check if Binary String Has at Most One Segment of Ones - check if a binary string has at most one contiguous segment of ones. The function returns true if the ones form a single unbroken sequence; otherwise, it returns false.
https://github.com/emilwijayasekara/leetcode-1784-check-if-binary-string-has-at-most-one-segment-of-ones
java leetcode leetcode-java leetcode-solutions
Last synced: about 2 months ago
JSON representation
LeetCode Problem 1784. Check if Binary String Has at Most One Segment of Ones - check if a binary string has at most one contiguous segment of ones. The function returns true if the ones form a single unbroken sequence; otherwise, it returns false.
- Host: GitHub
- URL: https://github.com/emilwijayasekara/leetcode-1784-check-if-binary-string-has-at-most-one-segment-of-ones
- Owner: EmilWijayasekara
- Created: 2024-01-07T09:13:38.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-07T09:20:47.000Z (about 1 year ago)
- Last Synced: 2024-01-07T10:38:09.494Z (about 1 year ago)
- Topics: java, leetcode, leetcode-java, leetcode-solutions
- Language: Java
- Homepage: https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/description/
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# LeetCode Practice (Day 20)
## Achievements
[![Leetcode-copy.jpg](https://i.postimg.cc/s2w0PV0q/Leetcode-copy.jpg)](https://postimg.cc/ns9kptxG)
## About the problem
- *Problem Number* : 1784
- *Problem Name* : [Check if Binary String Has at Most One Segment of Ones](https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/description/ "https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/description/")
- *Problem difficulty* : Easy π’
- *Programming language used* - Java## Problem
Given a binary string `s` **βββββwithout leading zeros**, return `true`βββ _if_ `s` _contains **at most one contiguous segment of ones**_. Otherwise, return `false`.
**Example 1:**
```
Input: s = "1001"
Output: false
Explanation: The ones do not form a contiguous segment.
```**Example 2:**
```
Input: s = "110"
Output: true
```**Constraints:**
- `1 <= s.length <= 100`
- `s[i]`ββββ is either `'0'` or `'1'`.
- `s[0]` is `'1'`.## Approach Explanation
Checking whether the binary string contains the substring "01". If this substring is present, it indicates the existence of at least two contiguous segments of ones, and the function returns `false`. Conversely, if the substring is not found, it implies that the ones in the string form a single, unbroken sequence, and the function returns `true`. My approach prioritizes simplicity and readability by utilizing the `contains` method to directly assess the presence of the specified substring, offering a concise solution to the problem.
### If you have suggestions for improvement or would like to contribute to this solution, feel free to create a pull request. ππ