-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1784-CheckIfBinaryStringHasAtMostOneSegmentOfOnes.go
More file actions
95 lines (82 loc) · 2.92 KB
/
1784-CheckIfBinaryStringHasAtMostOneSegmentOfOnes.go
File metadata and controls
95 lines (82 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
// 1784. Check if Binary String Has at Most One Segment of Ones
// 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'.
import "fmt"
import "strings"
func checkOnesSegment(s string) bool {
if s == "1"{ return true }
arr := []int{}
for i, v := range s {
if v == '1' {
arr = append(arr, i)
}
}
sum, z, a, b := 0, 0, arr[0], arr[len(arr) - 1]
for i := a; i <= b; i++{
z += i
}
for _, v := range arr {
sum += v
}
return z == sum
}
func checkOnesSegment1(s string) bool {
return !strings.Contains(s, "01")
}
func checkOnesSegment2(s string) bool {
for i := 1; i < len(s); i++ {
if s[i - 1] == '0' && s[i] == '1' { return false }
}
return true
}
func main() {
// Example 1:
// Input: s = "1001"
// Output: false
// Explanation: The ones do not form a contiguous segment.
fmt.Println(checkOnesSegment("1001")) // false
// Example 2:
// Input: s = "110"
// Output: true
fmt.Println(checkOnesSegment("110")) // true
fmt.Println(checkOnesSegment("1")) // true
fmt.Println(checkOnesSegment("10")) // true
fmt.Println(checkOnesSegment("0000000000")) // true
fmt.Println(checkOnesSegment("1111111111")) // true
fmt.Println(checkOnesSegment("0000011111")) // false
fmt.Println(checkOnesSegment("1111100000")) // true
fmt.Println(checkOnesSegment("0101010101")) // false
fmt.Println(checkOnesSegment("1010101010")) // false
fmt.Println(checkOnesSegment1("1001")) // false
fmt.Println(checkOnesSegment1("110")) // true
fmt.Println(checkOnesSegment1("1")) // true
fmt.Println(checkOnesSegment1("10")) // true
fmt.Println(checkOnesSegment1("0000000000")) // true
fmt.Println(checkOnesSegment1("1111111111")) // true
fmt.Println(checkOnesSegment1("0000011111")) // false
fmt.Println(checkOnesSegment1("1111100000")) // true
fmt.Println(checkOnesSegment1("0101010101")) // false
fmt.Println(checkOnesSegment1("1010101010")) // false
fmt.Println(checkOnesSegment2("1001")) // false
fmt.Println(checkOnesSegment2("110")) // true
fmt.Println(checkOnesSegment2("1")) // true
fmt.Println(checkOnesSegment2("10")) // true
fmt.Println(checkOnesSegment2("0000000000")) // true
fmt.Println(checkOnesSegment2("1111111111")) // true
fmt.Println(checkOnesSegment2("0000011111")) // false
fmt.Println(checkOnesSegment2("1111100000")) // true
fmt.Println(checkOnesSegment2("0101010101")) // false
fmt.Println(checkOnesSegment2("1010101010")) // false
}