#K73867. Count Substring Occurrences
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"
andsubString = "abra"
, the answer is 2. - For
mainString = "hello"
andsubString = "world"
, the answer is -1. - For
mainString = "aaa"
andsubString = "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:
- The first line contains the string
mainString
. - 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.
abracadabra
abra
2