jasonpogi1669

Count "yo" in a String Problem using Java

May 25th, 2021
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class Main {
  4.   static int hjtthegreat(String s, int id, int cnt, boolean[] visited) {
  5.     if (id + 1 > (int) s.length()) {
  6.       return cnt;
  7.     }
  8.     if (s.charAt(id) == 'y' && s.charAt(id + 1) == 'o') {
  9.         visited[id] = visited[id + 1] = true;
  10.         if (id == 0 || s.charAt(id - 1) != 'o' || visited[id - 1]) {
  11.             return hjtthegreat(s, id + 1, cnt + 1, visited);
  12.         }
  13.     }
  14.     return hjtthegreat(s, id + 1, cnt, visited);
  15.   }
  16.  
  17.   public static void main(String[] args) {
  18.     String[] s = {"ayoxyoxyoxyoxyo", "ayobyoayobhadjipogiyoayobyo",
  19.                 "xyoxhadjiohiyoyoyo", "yoyooyoxhadjiohioyooyoyoxxyoyo"};
  20.     int id = 0;
  21.     int cnt = 0;
  22.     boolean[] visited;
  23.     for (int i = 0; i < (int) s.length; i++) {
  24.       visited = new boolean[(int) s[i].length()];
  25.       Arrays.fill(visited, false);
  26.       System.out.println(s[i] + (i % 2 == 0 ? "=" : ":") + hjtthegreat(s[i], id, cnt, visited));
  27.     }
  28.   }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment