forked from mirandaio/codingbat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgHappy.java
More file actions
23 lines (19 loc) · 770 Bytes
/
gHappy.java
File metadata and controls
23 lines (19 loc) · 770 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* We'll say that a lowercase 'g' in a string is "happy" if there is another
* 'g' immediately to its left or right. Return true if all the g's in the
* given string are happy.
*/
public boolean gHappy(String str) {
if(str.length() == 1 && str.charAt(0) == 'g')
return false;
if(str.length() >= 2 &&
(str.charAt(0) == 'g' && str.charAt(1) != 'g' ||
str.charAt(str.length()-1) == 'g' &&
str.charAt(str.length()-2) != 'g'))
return false;
for(int i = 1; i <= str.length() - 2; i++) {
if(str.charAt(i) == 'g' && str.charAt(i-1) != 'g' &&
str.charAt(i+1) != 'g')
return false;
}
return true;
}