Problem:
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.
gHappy("xxggxx") → true
gHappy("xxgxx") → false
gHappy("xxggyygxx") → false
Solution:
public boolean gHappy(String str) { int len = str.length(); boolean happy = true; for (int i = 0; i < len; i++) { if (str.charAt(i) == 'g') { if (i > 0 && str.charAt(i-1) == 'g') happy = true; else if (i < len-1 && str.charAt(i+1) == 'g') happy = true; else happy = false; } } return happy; }
This is shit. Better code (which works):
ReplyDeletepublic boolean gHappy(String str) {
str = str.replaceAll("ggg", "");
str = str.replaceAll("gg", "");
return !str.contains("g");
}
That's a very smart and short way to solve this problem. Happy coding :)
Deletegreat idea however, it wouldn't work for gggg. as the first replaceAll method would leave a remaining g. instead you could use regex "g{2,}" to find all occurrences of 2 or more g's.
DeleteWrong, you must follow the rules of the exercise and use loops.
DeleteWrong, it doesn't work for xxggggxx
Deletedoes not work for "gagg"
ReplyDeleteYes! codingbat.com missing this use case.
DeleteIf you comment out lines 8 and 10, it will work with "gagg". Alternatively, you could get rid of the 'happy' variable:
Deletepublic boolean gHappy(String str) {
int len = str.length();
for (int i = 0; i < len; i++) {
if (str.charAt(i) == 'g'
&& !(i > 0 && str.charAt(i-1) == 'g')
&& !(i < len-1 && str.charAt(i+1) == 'g')) {
return false;
}
}
return true;
}
public boolean gHappy(String str) {
ReplyDeletestr="x"+str+"x";
for(int i=1;i<str.length();i++){
if(str.substring(i,i+1).equals("g")&&str.substring(i-1,i+2).indexOf("gg")==-1)
return false;}
return true;
}
On line 12: happy = false; replace with return false;
ReplyDeleteIf you find first alone "g" than return false because like in an example "gagg" after first "g" happy will be false and than at the end "gg" will change happy to true
public boolean gHappy(String str) {
ReplyDeleteString add="+"+str+"+";
String check = "";
for (int i=1; i<add.length(); i++)
{
if (add.charAt(i)!='g') check = check + "2";
else if ((add.charAt(i)=='g' && add.charAt(i+1)=='g') ||
(add.charAt(i)=='g' && add.charAt(i-1)=='g'))
check = check + "1";
else check = check + "0";
}
return (check.indexOf("0")<0);
}
I don't understand anything.
ReplyDeletepublic boolean gHappy(String str) {
ReplyDeleteint l = str.length();
str = "x" + str + "x";
for (int i = 1; i<=l; i++) {
if (str.charAt(i) == 'g' && str.charAt(i+1) != 'g' && str.charAt(i-1) != 'g') return false;
}
return true;
}
boolean ishappy = true;
ReplyDeleteint len = str.length();
if (len == 1 && str.charAt(0) == 'g') return false;
for (int i=str.length();i>0;i--){
while ( i < len-1 && str.charAt(i) == 'g' && str.charAt(i+1) != 'g' && str.charAt(i-1) != 'g'
|| i < len-2 && str.charAt(len-1) == 'g' && str.charAt(len-2) != 'g'){
return ishappy = false;
}
}
return ishappy;
public boolean gHappy(String str) {
ReplyDeleteif(str.length()==0) return true;
if(str.length()==1) return false;
if(str.charAt(0)=='g' && str.charAt(1)!='g') return false;
if(str.charAt(str.length()-1)=='g' && str.charAt(str.length()-2)!='g') return false;
for(int i=1;i<str.length()-1;i++){
if((str.charAt(i)=='g' && str.charAt(i-1)!='g') && (str.charAt(i)=='g' && str.charAt(i+1)!='g')){
return false;
}
}
return true;
}
return !str.matches("(.*)[^g]g[^g](.*)|g|(.*)[^g]g|g(^g)(.*)");
ReplyDeleteThat is genius
Delete
ReplyDeletepublic static boolean gHappy(String str) {
int len = str.length();
for (int i = 0; i < len; i++) {
if (str.charAt(i) == 'g') {
if (i > 0 && str.charAt(i - 1) == 'g')
continue;
else if (i < len - 1 && str.charAt(i + 1) == 'g')
continue;
else
return false;
}
}
return true;
}
this works find.
you miss g which there is another gg after that
public boolean gHappy(String str) {
ReplyDeleteif (str.length() == 1)
{
return false;
}
for (int i = 0; i < str.length(); i++)
{
int count = 0;
while (i < str.length() && str.charAt(i) == 'g')
{
count++;
i++;
}
if (count == 1)
{
return false;
}
}
return true;
}
public boolean gHappy(String str) {
ReplyDeletereturn !str.replaceAll("ggg", "").replaceAll("gg", "").contains("g");
}
THIS CONDITION WORKS FOR "gggg", "ggggg" (for any num of continuous g's)
ReplyDeletepublic boolean gHappy(String str)
{
str = str.replace("gg", "++");
str = str.replace("+g", "++");
str = str.replace("g", "-");
int count = 0;
for(int i = 0; i 0)
{
return false;
}
return true;
}
CORRECT CODE:
Deletepublic boolean gHappy(String str)
{
str = str.replace("gg", "++");
str = str.replace("+g", "++");
str = str.replace("g", "-");
int count = 0;
for(int i = 0; i 0)
{
return false;
}
return true;
}
public boolean gHappy(String str) {
ReplyDeleteString str1=str.replaceAll("gg","1");
String str2=str1.replaceAll("1g","1");
String str3=str2.replaceAll("g1","1");
boolean happy=true;
int n=str3.length();
for(int i=0;i<n;i++){
if(str3.charAt(i)=='g'){
happy=false;
break;
}
}
return happy;
}
public boolean gHappy(String str) {
ReplyDeleteint countOne = 0;
int countTwo = 0;
for (int i = str.length()-1; i>=0; i--) {
if (str.charAt(i) == 'g') countTwo++;
if (i > 0 && str.charAt(i) == 'g' && str.charAt(i - 1) == 'g') countOne++;
if (countTwo - 2 * countOne > 0) return false;
}
return true;
}
public boolean gHappy(String str) {
ReplyDeleteboolean a=true;
int t=str.length();
for(int i=0;i0 &&str.charAt(i-1)=='g')
a=true;
else if(i<t-1 &&str.charAt(i+1)=='g')
a=true;
else a=false;
}
}return a;
}
There are much better ones on here, a couple I do not understand at all in fact. But what I came up with shows logic as it happens.
ReplyDeletepublic boolean gHappy(String str) {
boolean result = true;
for (int i = 0; i < str.length(); ++i) {
boolean isG = (str.charAt(i) == 'g');
boolean preG = (i != 0 && str.charAt(i-1) == 'g');
boolean postG = (i != str.length()-1 && str.charAt(i+1) == 'g');
if (isG && (!preG && !postG)) result = false;
}
return result;
}
public boolean gHappy(String str) {
ReplyDeleteif(str.length() == 0)
return true;
boolean flag = false;
for(int i = 1; i < str.length(); i++){
if(str.charAt(i) == 'g'){
if(str.charAt(i-1) == 'g'){
flag = true;
i++;
}
else
flag = false;
}
}
return flag;
}