Advertisement
Slyke

H221

Jul 26th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. import java.util.*; public class H221 {public static void main (String[] args) {while (JPL.test()) {
  2.  
  3. ////////////////////////////// PROBLEM STATEMENT //////////////////////////////
  4. // We'll say that a "nsequence" in a string is a char appearing n times in   //
  5. // a row. Given a string and an integer n, print the number of nsequences in //
  6. // the given string. The nsequences may overlap. Use a method to count the   //
  7. // number of nsequences.                                                     //
  8. //   "abcXXXabc" 3 -> 1                                                      //
  9. //   "xxxabyyyycd" 3 -> 3                                                    //
  10. //   "a" 2 -> 0                                                              //
  11. ///////////////////////////////////////////////////////////////////////////////
  12.  
  13.   // >>>>>> Your Java Code Fragment starts here <<<<<<
  14.  
  15.   Scanner kb = new Scanner(System.in);
  16.  
  17.   String checkingData=kb.next();
  18.   int checkFor=kb.nextInt();
  19.  
  20.   System.out.print(findAmount(checkingData, checkFor));
  21.  
  22.   // >>>>>> Your Java Code Fragment ends here <<<<<<
  23. }}
  24.  
  25. // >>>>>> Your Java Methods start here <<<<<<
  26.  
  27. static int findAmount(String search, int howMany)
  28. {
  29.   int stringLength=search.length();
  30.  
  31.   int countTracker=0;
  32.   int skipCount=0;
  33.  
  34.   for (int i=0; i<stringLength-1;i++)
  35.   {
  36.     if (search.charAt(i)==search.charAt(i+1))
  37.     {
  38.       skipCount=0;
  39.       for (int j=i;j<stringLength;j++)
  40.       {
  41.         if (search.charAt(i)==search.charAt(j))
  42.         {
  43.           skipCount++;
  44.         }
  45.         else
  46.         {
  47.           break;
  48.         }
  49.       }
  50.       if (skipCount >= howMany) { countTracker++; }
  51.      
  52.     }
  53.   }
  54.  
  55.   return countTracker;
  56.  
  57. }
  58.  
  59. // >>>>>> Your Java Methods end here <<<<<<
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement