#K73867. Count Substring Occurrences

    ID: 34071 Type: Default 1000ms 256MiB

Count Substring Occurrences

Count Substring Occurrences

You are given a non-empty string mainString and a second string subString. Your task is to determine how many times subString appears as a contiguous subsequence in mainString in a non-overlapping manner. If subString does not appear in mainString, output -1.

For example:

  • For mainString = "abracadabra" and subString = "abra", the answer is 2.
  • For mainString = "hello" and subString = "world", the answer is -1.
  • For mainString = "aaa" and subString = "a", the answer is 3.

Note: Occurrences are counted in a non-overlapping manner. That is, once a match is found, the search continues after the match.

inputFormat

The input consists of two lines:

  1. The first line contains the string mainString.
  2. The second line contains the string subString.

outputFormat

Output a single integer representing the number of times subString appears in mainString as a contiguous subsequence, or -1 if it does not appear.

## sample
abracadabra
abra
2