Advertisement
Spencer-Studios

equals is not [java task - solution]

Jun 23rd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. public class PlayTime {
  2.  
  3.     public static void main(String[] args) {
  4.  
  5.         String string = "isnotis xxnotx isnononot";
  6.         System.out.println(equalsIsNot(string));
  7.     }
  8.  
  9.     static boolean equalsIsNot(String string) {
  10.  
  11.         int nIs = countOccurences(string, "is");
  12.         int nNot = countOccurences(string, "not");
  13.  
  14.         return nIs == nNot;
  15.     }
  16.  
  17.     //helper method...
  18.     static int countOccurences(String str, String sub) {
  19.  
  20.         int count = 0, len = sub.length();
  21.  
  22.         for (int i = 0; i <= (str.length() - len); i++) {
  23.             String it = str.substring(i, i + len);
  24.             if (it.equals(sub)) {
  25.                 count++;
  26.             }
  27.         }
  28.         return count;
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement