Problem:
We'll say that a String is xy-balanced if for all the 'x' chars in the string, there exists a 'y' char somewhere later in the string. So "xxy" is balanced, but "xyx" is not. One 'y' can balance multiple 'x's. Return true if the given string is xy-balanced.
xyBalance("aaxbby") → true
xyBalance("aaxbb") → false
xyBalance("yaaxbb") → false
Solution:
public boolean xyBalance(String str) { Boolean x = false; Boolean y = false; int len = str.length(); for (int i = 0; i < len; i++) { if (str.charAt(i) == 'x' && y == true){ x = true; y = false; } else if (str.charAt(i) == 'x') { x = true; } if (str.charAt(i) == 'y' && x == true) y = true; } if (x == false) y = true; return y; }
public boolean xyBalance(String str) {
ReplyDeleteboolean match = false;
if(str.length() == 1 && str.contains("y")){
match = true;
}
if(str.indexOf('x') == -1 && str.indexOf('y') == -1){
match = true;
}
if(str.contains("x") && str.contains("y")){
if(str.lastIndexOf('y') > str.lastIndexOf('x')){
match = true;
}
}
return match;
}
public boolean xyBalance(String str) {
ReplyDeletereturn (str.indexOf('x')==-1) ||
str.lastIndexOf('x') < str.lastIndexOf('y');
}
Good bro thanx.
Deletepublic boolean xyBalance(String str) {
ReplyDeleteboolean y = true;
for(int i = 0; i < str.length(); i++) {
if(Character.toString(str.charAt(i)).equals("x") && y) {
y = false;
} else if(Character.toString(str.charAt(i)).equals("y") && !y) {
y = true;
}
}
return y;
}
int count = 0;
ReplyDeletefor (int i=0; i<str.length(); i++) {
if (count == 0 && str.charAt(i) == 'x') count++;
if (count !=0 && str.charAt(i) == 'y') count--;
}
if (count == 0) return true;
return false;
Great!
Deletepublic boolean xyBalance(String str) {
ReplyDeleteint no_of_y = 0;
int no_of_x = 0;
if(str.equals("x")){
return false;
}
if(str.equals("y")){
return true;
}
for(int count=0; count0 && no_of_x==0){
return true;
}
if(no_of_x>0 && no_of_y==0){
return false;
}
if(no_of_y>0 && no_of_x>0 && str.lastIndexOf("y")>str.lastIndexOf("x")){
return true;
}
if(no_of_y>0 && no_of_x>0 && str.lastIndexOf("x")>str.lastIndexOf("y")){
return false;
}
return true;
}
return str.matches("(^$)|([^Xx]*)|(.*[Xx].*[Yy][^Xx]*)");
ReplyDeleteNice!
Deletepublic boolean xyBalance(String str) {
ReplyDeletestr = str.toLowerCase();
for(int i=0; i<str.length(); i++){
if(str.charAt(str.length()-1)=='y') return true;
else if(str.charAt(str.length()-1)=='x') return false;
else str=str.substring(0, str.length()-1);
}
return true;
}
public boolean xyBalance(String str)
ReplyDelete{
if(str.lastIndexOf("y")<str.lastIndexOf("x"))
{
return false;
}
return true;
}
Very good
Deletepublic boolean xyBalance(String str) {
ReplyDeleteint y = str.lastIndexOf('y');
int x = str.lastIndexOf('x');
return(x < y || !str.contains("x"));
}
char[] ch=str.toCharArray();
ReplyDeleteint len=str.length();
if(str.length()==0)
{
return true;
}
if(len==3)
{
if(str.charAt(0)!='x'&&str.charAt(1)!='x'&&str.charAt(2)!='x')
{
return true;
}
}
if(len==1)
{
if(str.charAt(0)=='y')
{
return true;
}
}
if(len==2)
{
if(str.charAt(1)=='y'||str.charAt(0)=='y')
{
return true;
}
}
if(str.charAt(len-1)=='x')
{
return false;
}
for(int i=0;i<str.length()-2;i++)
{
if(str.charAt(i)=='y'&&str.charAt(len-1)!='y')
{
return false;
}
if(ch[i]=='x'&&str.contains("y"))
{
return true;
}
}
return false;
public boolean xyBalance(String str) {
ReplyDeleteint x = str.lastIndexOf('x');
int y = str.lastIndexOf('y');
return (x > y) ? false : true;
}
public boolean xyBalance(String str) {
ReplyDeleteif(!str.contains("x") && !str.contains("y")) return true;
else if(str.lastIndexOf("y") > str.lastIndexOf("x")) return true;
else return false;
}
public String mixString(String a, String b) {
ReplyDeleteString result = "";
int len = Math.max(a.length(), b.length());
for(int i = 0; i < len; i++){
if(a.length()>i) result += a.charAt(i);
if(b.length()>i) result += b.charAt(i);
}
return result;
}
public boolean xyBalance(String str) {
ReplyDeleteboolean result = true;
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) == 'x'){
result = str.substring(i).indexOf('y') == -1 ? false : true;
}
}
return result;
}
A total lastIndexOf() method:
ReplyDeletepublic boolean xyBalance(String str) {
if(str==null || str.length()==0) return true;
if(str.lastIndexOf('x')<0 && str.lastIndexOf('y') >= 1) return false;
if(str.lastIndexOf('x')<0 ) return true;
if(str.lastIndexOf('y')<0 ) return false;
if(str.lastIndexOf('x') < str.lastIndexOf('y')) return true;
return false;
}
public boolean xyBalance(String str) {
ReplyDeleteint x =str.lastIndexOf("x");
int y =str.lastIndexOf("y");
if(x<=y)
return true;
return false;
}
if(str.lastIndexOf('y')<str.lastIndexOf('x'))return false;
ReplyDeletereturn true;
find(x(y))
ReplyDeletegive value x
if y(x)
return true;
return false;
// IMDAD MOMIN
ReplyDeletereturn (str.lastIndexOf('x') < str.lastIndexOf('y') || !str.contains("x"));
//OR
return (str.lastIndexOf('x') < str.lastIndexOf('y') || str.indexOf('x')==-1);
public boolean xyBalance(String str) {
ReplyDeletefor (int i = 0; i < str.length(); i++)
{
if (str.charAt(i) == 'x')
{
while (i < str.length() && str.charAt(i) != 'y')
{
i++;
}
if (i == str.length())
{
return false;
}
}
}
return true;
}
public boolean xyBalance(String str)
ReplyDelete{
return str.lastIndexOf("y")>=str.lastIndexOf("x");
}
public boolean xyBalance(String str) {
ReplyDeleteif(str.lastIndexOf("y")<str.lastIndexOf("x"))
{
return false;
}
return true;.
}
Given a string, consider the prefix string made of the first N chars of the string. Does that prefix string appear somewhere else in the string? Assume that the string is not empty and that N is in the range 1..str.length().
ReplyDeletepublic boolean xyBalance(String str) {
ReplyDeleteint x = str.lastIndexOf("x");
int y = str.lastIndexOf("y");
if(str.length()<=0 || (!str.contains("y") && !str.contains("x"))){
return true;
}
if(y>x){
return true;
}
return false;
}
public boolean xyBalance(String str)
ReplyDelete{
return!(str.lastIndexOf("y")<str.lastIndexOf("x"));
}
public boolean xyBalance(String str) {
ReplyDeleteint xCount = 0;
int yCount = 0;
int xSpot = 0;
int ySpot = 0;
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) == 'x'){
xCount++;
xSpot = i;
}
if(str.charAt(i) == 'y'){
yCount++;
ySpot = i;
}
}
if(ySpot > xSpot && yCount > 0) return true;
if(xCount == 0) return true;
return false;
}
public boolean xyBalance(String str) {
ReplyDeleteif(!str.contains("x")) return true ;
for(int x = str.length()-1 ; x >= 0 ; x--){
if(str.charAt(x) == 'x'){
return (str.substring(x,str.length()).contains("y"));
}
}
return false;
}
public boolean xyBalance(String str) {
ReplyDeleteif(!(str.contains("x"))){
return true;
}
int i= str.lastIndexOf("x");
int j= str.lastIndexOf("y");
if(j>i){
return true;
}
return false;
}
public boolean xyBalance(String str) {
ReplyDeleteint t=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='x'){
t=1;
}else if(str.charAt(i)=='y'){
t=0;
}
}
return (t==0);
}
one faster way and cleaner method that is satisfy by eclipse:
ReplyDeletepublic boolean xyBalance(String str) {
int count = 0, x = str.length();
for (int i = 0; i < x; i++) {
if (count == 0 && str.charAt(i) == 'x')
count++;
if (count != 0 && str.charAt(i) == 'y')
count--;
}
if (count == 0) {
return true;
}
return false;
}