7.6k views
2 votes
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

User CovertIII
by
7.9k points

1 Answer

1 vote

Answer:

Check the explanation

Step-by-step 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.

Write a method that uses a stack to determine if a string has an amount divisible-example-1
User Very Very Cherry
by
7.9k points