Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*; public class H221 {public static void main (String[] args) {while (JPL.test()) {
- ////////////////////////////// PROBLEM STATEMENT //////////////////////////////
- // We'll say that a "nsequence" in a string is a char appearing n times in //
- // a row. Given a string and an integer n, print the number of nsequences in //
- // the given string. The nsequences may overlap. Use a method to count the //
- // number of nsequences. //
- // "abcXXXabc" 3 -> 1 //
- // "xxxabyyyycd" 3 -> 3 //
- // "a" 2 -> 0 //
- ///////////////////////////////////////////////////////////////////////////////
- // >>>>>> Your Java Code Fragment starts here <<<<<<
- Scanner kb = new Scanner(System.in);
- String checkingData=kb.next();
- int checkFor=kb.nextInt();
- System.out.print(findAmount(checkingData, checkFor));
- // >>>>>> Your Java Code Fragment ends here <<<<<<
- }}
- // >>>>>> Your Java Methods start here <<<<<<
- static int findAmount(String search, int howMany)
- {
- int stringLength=search.length();
- int countTracker=0;
- int skipCount=0;
- for (int i=0; i<stringLength-1;i++)
- {
- if (search.charAt(i)==search.charAt(i+1))
- {
- skipCount=0;
- for (int j=i;j<stringLength;j++)
- {
- if (search.charAt(i)==search.charAt(j))
- {
- skipCount++;
- }
- else
- {
- break;
- }
- }
- if (skipCount >= howMany) { countTracker++; }
- }
- }
- return countTracker;
- }
- // >>>>>> Your Java Methods end here <<<<<<
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement