
You are given an array of strings, arr, containing N strings, and a non-empty target substring, target. Your task is to find and return the string from the array that contains the highest number of occurrences of the target substring.
There are two important rules to consider:
The first line contains a single integer N, the number of strings in the array.
The second line contains the target substring.
The next N lines each contain one string from the arr array.
Print a single line containing the string from the array that has the maximum number of occurrences of the target, according to the rules.
A simple approach using a split() function on the target string may not work correctly due to the overlapping rule. A more robust method is to iteratively search for the substring from the position immediately after the last found match.
3
ap
apple
banana
apricot
apple
"apple" contains "ap" 1 time.
"banana" contains "ap" 0 times.
"apricot" contains "ap" 1 time.
There is a tie between "apple" and "apricot". According to the tie-breaking rule, the one that appeared first, "apple", is returned.
2
aba
ababa
abab
ababa
"ababa" contains "aba" 2 times (at index 0 and 2).
"abab" contains "aba" 1 time (at index 0).
"ababa" has the clear maximum number of occurrences.
The expected time complexity is O(N^3).
1 <= N <= 1000
1 <= length of each string in arr <= 1000
1 <= length of target <= 1000
All strings consist of lowercase English letters.
Time limit: 1 sec