-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2124-CheckIfAllAsAppearsBeforeAllBs.go
More file actions
69 lines (61 loc) · 1.86 KB
/
2124-CheckIfAllAsAppearsBeforeAllBs.go
File metadata and controls
69 lines (61 loc) · 1.86 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
package main
// 2124. Check if All A's Appears Before All B's
// Given a string s consisting of only the characters 'a' and 'b',
// return true if every 'a' appears before every 'b' in the string.
// Otherwise, return false.
// Example 1:
// Input: s = "aaabbb"
// Output: true
// Explanation:
// The 'a's are at indices 0, 1, and 2, while the 'b's are at indices 3, 4, and 5.
// Hence, every 'a' appears before every 'b' and we return true.
// Example 2:
// Input: s = "abab"
// Output: false
// Explanation:
// There is an 'a' at index 2 and a 'b' at index 1.
// Hence, not every 'a' appears before every 'b' and we return false.
// Example 3:
// Input: s = "bbb"
// Output: true
// Explanation:
// There are no 'a's, hence, every 'a' appears before every 'b' and we return true.
// Constraints:
// 1 <= s.length <= 100
// s[i] is either 'a' or 'b'.
import "fmt"
func checkString(s string) bool {
flag := false
for _, v := range s {
if v == 'a' {
if flag {
return false
}
} else {
flag = true
}
}
return true
}
func main() {
// Example 1:
// Input: s = "aaabbb"
// Output: true
// Explanation:
// The 'a's are at indices 0, 1, and 2, while the 'b's are at indices 3, 4, and 5.
// Hence, every 'a' appears before every 'b' and we return true.
fmt.Println(checkString("aaabbb")) // true
// Example 2:
// Input: s = "abab"
// Output: false
// Explanation:
// There is an 'a' at index 2 and a 'b' at index 1.
// Hence, not every 'a' appears before every 'b' and we return false.
fmt.Println(checkString("abab")) // false
// Example 3:
// Input: s = "bbb"
// Output: true
// Explanation:
// There are no 'a's, hence, every 'a' appears before every 'b' and we return true.
fmt.Println(checkString("bbb")) // true
}