(Coderbyte) Ex Oh - Solução

Have the function ExOh(str) take the str parameter being passed and return the string true if there is an equal number of x's and o's, otherwise return the string false. Only these two letters will be entered in the string, no punctuation or numbers. For example: if str is "xooxxxxooxo" then the output should return false because there are 6 x's and 5 o's.

Solução:

import java.util.*;
import java.io.*;

class Function { 
  String ExOh(String str) {
 
    int tamDaString = str.length();
    if ((tamDaString%2) != 0) {
        return "false";
    }
   
    int counts = 0;
    for (int i = 0; i < tamDaString; i++) {
      if (str.charAt(i) == 'x') {
          counts++;
      }
      else {
        counts--;
      }
    }
   
    if (counts == 0) {
      return "true";
    }
    else {
      return "false";
    }
   
  }
 
  public static void main (String[] args) { 
    // keep this function call here    
    Scanner  s = new Scanner(System.in);
    Function c = new Function();
    System.out.print(c.ExOh(s.nextLine()));
  }  
 
}          

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Powers of Two - Solução

(CoderByte) Number Search - Solução