-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckString-LCQ.cpp
More file actions
54 lines (39 loc) · 1.06 KB
/
CheckString-LCQ.cpp
File metadata and controls
54 lines (39 loc) · 1.06 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
/*
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'.
*/
class Solution {
public:
bool checkString(string s) {
int prev, next;
for (int i = 1; i < s.size(); i++) {
prev = s[i - 1] - 'a' + 1;
next = s[i] - 'a' + 1;
if (prev > next) {
return false;
break;
}
}
return true;
}
};