Java > String-2 > xyBalance (CodingBat Solution)

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;
}


34 comments :

  1. public boolean xyBalance(String str) {
    boolean 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;
    }

    ReplyDelete
  2. public boolean xyBalance(String str) {

    return (str.indexOf('x')==-1) ||
    str.lastIndexOf('x') < str.lastIndexOf('y');
    }

    ReplyDelete
  3. public boolean xyBalance(String str) {
    boolean 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;
    }

    ReplyDelete
  4. int count = 0;

    for (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;

    ReplyDelete
  5. public boolean xyBalance(String str) {
    int 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;
    }

    ReplyDelete
  6. return str.matches("(^$)|([^Xx]*)|(.*[Xx].*[Yy][^Xx]*)");

    ReplyDelete
  7. public boolean xyBalance(String str) {
    str = 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;
    }

    ReplyDelete
  8. public boolean xyBalance(String str)
    {
    if(str.lastIndexOf("y")<str.lastIndexOf("x"))
    {
    return false;
    }
    return true;
    }

    ReplyDelete
  9. public boolean xyBalance(String str) {
    int y = str.lastIndexOf('y');
    int x = str.lastIndexOf('x');
    return(x < y || !str.contains("x"));
    }

    ReplyDelete
  10. char[] ch=str.toCharArray();
    int 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;

    ReplyDelete
  11. public boolean xyBalance(String str) {
    int x = str.lastIndexOf('x');
    int y = str.lastIndexOf('y');

    return (x > y) ? false : true;
    }

    ReplyDelete
  12. public boolean xyBalance(String str) {
    if(!str.contains("x") && !str.contains("y")) return true;
    else if(str.lastIndexOf("y") > str.lastIndexOf("x")) return true;
    else return false;
    }

    ReplyDelete
  13. public String mixString(String a, String b) {
    String 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;
    }

    ReplyDelete
  14. public boolean xyBalance(String str) {
    boolean 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;
    }

    ReplyDelete
  15. A total lastIndexOf() method:
    public 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;
    }

    ReplyDelete
  16. public boolean xyBalance(String str) {
    int x =str.lastIndexOf("x");
    int y =str.lastIndexOf("y");
    if(x<=y)
    return true;
    return false;
    }

    ReplyDelete
  17. if(str.lastIndexOf('y')<str.lastIndexOf('x'))return false;
    return true;

    ReplyDelete
  18. find(x(y))
    give value x
    if y(x)
    return true;
    return false;

    ReplyDelete
  19. // IMDAD MOMIN
    return (str.lastIndexOf('x') < str.lastIndexOf('y') || !str.contains("x"));

    //OR

    return (str.lastIndexOf('x') < str.lastIndexOf('y') || str.indexOf('x')==-1);

    ReplyDelete
  20. public boolean xyBalance(String str) {
    for (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;
    }

    ReplyDelete
  21. public boolean xyBalance(String str)
    {
    return str.lastIndexOf("y")>=str.lastIndexOf("x");
    }

    ReplyDelete
  22. public boolean xyBalance(String str) {

    if(str.lastIndexOf("y")<str.lastIndexOf("x"))
    {
    return false;
    }
    return true;.
    }

    ReplyDelete
  23. 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().

    ReplyDelete
  24. public boolean xyBalance(String str) {


    int 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;
    }

    ReplyDelete
  25. public boolean xyBalance(String str)
    {
    return!(str.lastIndexOf("y")<str.lastIndexOf("x"));
    }

    ReplyDelete
  26. public boolean xyBalance(String str) {
    int 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;
    }

    ReplyDelete
  27. public boolean xyBalance(String str) {
    if(!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;
    }

    ReplyDelete
  28. public boolean xyBalance(String str) {

    if(!(str.contains("x"))){
    return true;
    }

    int i= str.lastIndexOf("x");
    int j= str.lastIndexOf("y");

    if(j>i){
    return true;
    }
    return false;
    }

    ReplyDelete
  29. public boolean xyBalance(String str) {
    int 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);
    }

    ReplyDelete
  30. one faster way and cleaner method that is satisfy by eclipse:
    public 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;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact