Write a method that uses a stack to determine if a string has an amount divisible by 3 of a letter that is given as a parameter to the method.

Examples: Input String and Character => output
"zookeeper" and ‘o’ => false
"zookeeper" and ‘z’ => false
"zookeeper" and ’s’ => true
"zookeeper" ‘e’ => true

Respuesta :

Answer:

Check the explanation

Explanation:

CODE IN JAVA:

StackDemo.java file:

import java.util.Stack ;

public class StackDemo {

  public static boolean checkStringAmount(String s, char ch) {

      Stack<Character> stack = new Stack<>();

     

      for(int i = 0 ; i < s.length(); i++) {

          if(s.charAt(i) == ch) {

              stack.push(ch);

          }

      }

      return stack.size() % 3 == 0 ;

  }

  public static void main(String[] args) {

     

      System.out.println("checkStringAmount(\"zookeeper\", 'o') : " + checkStringAmount("zookeeper", 'o'));

      System.out.println("checkStringAmount(\"zookeeper\", 'z') : " + checkStringAmount("zookeeper", 'z'));

      System.out.println("checkStringAmount(\"zookeeper\", 's') : " + checkStringAmount("zookeeper", 's'));

      System.out.println("checkStringAmount(\"zookeeper\", 'e') : " + checkStringAmount("zookeeper", 'e'));

  }

}

Kindly check the attached image below to see the Code Output.

Ver imagen temmydbrain
RELAXING NOICE
Relax