Question

In: Computer Science

public static int countSubstrings​(java.lang.String t, java.lang.String s, boolean allowOverlap) Counts the number of times that one...

public static int countSubstrings​(java.lang.String t, java.lang.String s, boolean allowOverlap)

Counts the number of times that one string occurs as a substring in another, optionally allowing the occurrences to overlap. For example:

  • countSubstrings("aa", "aaaaa", false) returns 2
  • countSubstrings("aa", "aaaaa", true) returns 4
  • countSubstrings("aa", "ababab", true) returns 0

Parameters:

t - string we are looking for ("target")

s - string in which we are looking ("source")

allowOverlap - true if occurrences of t are allowed to overlap

Returns:

number of times t occurs in s as a substring

Solutions

Expert Solution

public class SubStrings {
   public static void main(String[] args) {
       System.out.println(countSubstrings("aa", "aaaaa", true));
       System.out.println(countSubstrings("aa", "aaaaa", false));
      
   }
   public static int countSubstrings(java.lang.String t, java.lang.String s, boolean allowOverlap) {
       int count=0;
       if(allowOverlap) {
           for(int i=0;i<s.length();i++) {
               if(s.substring(i).indexOf(t)!=-1)
                   count++;
           }
       }
       else {
           int index=0;
           for(int i=0;i<s.length();i++) {
               if(s.indexOf(t,index)!=-1) {
                   count++;
                   index+=t.length();
               }
           }
       }
       return count;
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

public boolean isPrime(int num){ // return true if the number is prime, false otherwise } public...
public boolean isPrime(int num){ // return true if the number is prime, false otherwise } public boolean isOdd(int num){ // return true if the number is odd, false otherwise } public String reverseMyString(String input){ // using a loop, reverse a string // i.e. input = "hello", reversed answer: "olleh" // i.e. input = "bye", reversed answer: "eyb" } public int pow(int base, int power){ // using for loop, calculate base raised to power // i.e. base = 2, power =...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its input parameter, we'll say that a SubArray 9,8,7is that three integers, 9,8,7values appearing decreasing order one by one in the array. Return true if the array does contain any SubArray “987” Notice that any 3 integers that presenting a decreasing by one order must return True. (987, 876,765,654, 543, 432,321, 210 are all True) Call this method in your main function, and pass in...
Consider the following two methods: public static boolean isTrue(int n){ if (n <= 1)          return false;...
Consider the following two methods: public static boolean isTrue(int n){ if (n <= 1)          return false;        for (int i = 2; i < n; i++){          if (n % i == 0)             return false; }        return true; } public static int Method(int[] numbers, int startIndex) { if(startIndex >= numbers.length) return 0; if (isTrue(numbers[startIndex]))      return 1 + Method(numbers, startIndex + 1); else      return Method(numbers, startIndex + 1); } What is the final return value of Method() if it is called with the...
Required methods:: public static void processAccount (Account[] acc, printWriter out{}. public static boolean deposit(Account a){}.
Java programming. Required methods::public static void processAccount (Account[] acc, printWriter out{}.public static boolean deposit(Account a){}.public static boolean withdrawal(Account a){}.public static int findAccount(long accountNumber, Account[] acc){}. 1/ Remove the applyRandomBonus method from the test class2/ Create a File, Printwriter for an output file yourlastnameErrorLog.txt4/ Catch InputMismatch and ArrayIndexOutOfBounds exceptions when reading data from the file:a. Skip any lines that cause an exceptionb. Write information about the exception to the log file, yourlastnameError.txtc. Include exception type and line number in exception message...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the array to have the following property:        Suppose the first element in the original array has the value x.        In the new array, suppose that x is in position i, that is data[i] = x.        Then, data[j] <= x for all j < I and data[j] > x for all j > i.        Thus, informally, all the...
public static int punishOrMercy(char direction, int reward) Output: int which will be the value of zero...
public static int punishOrMercy(char direction, int reward) Output: int which will be the value of zero or the initial reward. Functionality: After the result of the reward function is stored, this function should be called. The goal of this function is to help the robot in case it faced a lot of damage in the current step. However, this function will help the robot only if the robot’s reward was negative and the direction that the user inputted was up....
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an...
(java)Write a recursive method public static int sumForEachOther(Int n) that takes a positive Int as an argument and returns the sum for every other Int from n down to 1. For example, sumForEachOther(8) should return 20, since 8+6+4+ 2=20.And the call sumForEachOther(9) should return 25 since 9+7+5 + 3+1-=25. Your method must use recursion.
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
public boolean areArrays(int[] arr1, int[] arr2){ // 1. initial check: both arrays need to have the...
public boolean areArrays(int[] arr1, int[] arr2){ // 1. initial check: both arrays need to have the same length, return false if not the same // 2. return true if both given arrays are equals(same values in the same indices), false otherwise if(arr1.length != arr2.length){ return false; } for(int i = 0; i < arr1.length; i++){ if(arr1[i] != arr2[i]){ } } return true; } public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return -1...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put code here return myint; } public int getInt() { int f = 0; // put code here return f; } } public class Assignment06 { public static void main(String args[]){ System.out.println("Assignmemnt 06 Written by Matt Weisfeld"); int myInt = 0; // create a Factorial object Factorial factorial = new Factorial(); // get a number from the console myInt = factorial.getInt(); System.out.println("Factorial of " +...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT