Skip to content

Commit b1a5fb3

Browse files
committed
1784. Check if Binary String Has at Most One Segment of Ones: AC
1 parent 649ffc7 commit b1a5fb3

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,3 +1344,4 @@ mod s1779_find_nearest_point_that_has_the_same_x_or_y_coordinate;
13441344
mod s1780_check_if_number_is_a_sum_of_powers_of_three;
13451345
mod s1781_sum_of_beauty_of_all_substrings;
13461346
mod s1782_count_pairs_of_nodes;
1347+
mod s1784_check_if_binary_string_has_at_most_one_segment_of_ones;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* [1784] Check if Binary String Has at Most One Segment of Ones
3+
*
4+
* Given a binary string s ​​​​​without leading zeros, return true​​​ if s contains at most one contiguous segment of ones. Otherwise, return false.
5+
*
6+
* Example 1:
7+
*
8+
* Input: s = "1001"
9+
* Output: false
10+
* Explanation: The ones do not form a contiguous segment.
11+
*
12+
* Example 2:
13+
*
14+
* Input: s = "110"
15+
* Output: true
16+
*
17+
* Constraints:
18+
*
19+
* 1 <= s.length <= 100
20+
* s[i]​​​​ is either '0' or '1'.
21+
* s[0] is '1'.
22+
*
23+
*/
24+
pub struct Solution {}
25+
26+
// problem: https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/
27+
// discuss: https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/discuss/?currentPage=1&orderBy=most_votes&query=
28+
29+
// submission codes start here
30+
31+
impl Solution {
32+
pub fn check_ones_segment(s: String) -> bool {
33+
s.chars()
34+
.skip_while(|val| *val == '1')
35+
.find(|val| *val == '1')
36+
.is_none()
37+
}
38+
}
39+
40+
// submission codes end
41+
42+
#[cfg(test)]
43+
mod tests {
44+
use super::*;
45+
46+
#[test]
47+
fn test_1784_example_1() {
48+
let s = "1001".to_string();
49+
50+
let result = false;
51+
52+
assert_eq!(Solution::check_ones_segment(s), result);
53+
}
54+
55+
#[test]
56+
fn test_1784_example_2() {
57+
let s = "110".to_string();
58+
59+
let result = true;
60+
61+
assert_eq!(Solution::check_ones_segment(s), result);
62+
}
63+
}

0 commit comments

Comments
 (0)