in the given string "UfUfUfufufufUfUf" each alternating string is considered as one item i.e UfUfUf is count 1, ufufuf is count 2, UfUf is count 3

Viewed 24

//in the given string "UfUfUfufufufUfUf" each alternating string is considered as one item i.e UfUfUf is count 1, ufufuf is count 2, UfUf is count 3. need a return statement to get appropriat output

import java.util.*;

public class Solution {

  static int peopleCount(String s) {


         int sum=0;
           int i = 0;
         
          
          while(i<s.length()-1) {
             char ch = s.charAt(i);
             if(ch=='U'|| ch=='u') {
                 sum++;
                 break;
             }
             i++;
         }
        
              
          return sum;
    
    
  }

  public static void main(String args[]) {

    assert (peopleCount("UfUfUf") == 1) : "Expect 1 for s=\"UfUfUf\"";
    System.out.println("All test cases in main function passed");

  }
}
0 Answers
Related