I was given 30 minutes to complete this test.
I didn't understand how the sample test cases arrived at their return output. Can anyone explain?
Approximate Matching (Coding)
You are given 3 strings: text, pre_text and post_text. Let L be a substring of text.
For each substring L of text, we define pattern_score as follows:
- pre_text_pattern_score = highest n, such that first n characters of L are equal to the last n characters of pre_text and occur in the same exact order.
- post_text_pattern_score = highest n such that last n characters of L are equal to the first n characters of post_text and occur in the same exact order.
- pattern_score = pre_text_pattern_score + post_text_pattern_score
For example, if L = "nothing", pre_text = "bruno", and post_text = "ingenious", then
- pre_text_pattern_score of L is 2 because the substring "no" is matched, and
- post_text_pattern_score is 3 because the substring "ing" is matched.
- pattern_score is 5 = 2 + 3
Your program should find a non-empty substring of text that maximizes pattern_score.
- If there is a tie, return the substring with the maximal pre_text_pattern_score.
- If multiple answers still have a tied score, return the answer that comes first lexicographically.
Complete the definition of function string calculateScore(string text, string prefix,string suffix)
Constraints:
- text, pre_text, and post_text contain only lowercase letters ('a' - 'z')
- 1 <= |text| <= 50
- 1 <= |pre-text| <= 50
- 1 <= |post-text| <= 50
(where |S| denotes the number of characters in string S)
- It is guaranteed that an answer will always exist; i.e. there will always be a substring in text that matches either at least one character at the end of pre-text or at least one character at the beginning of post-text.
Sample case #1
text: "nothing"
prefix: "bruno"
suffix: "ingenious"
Returns: "nothing"
Sample case #2
text: "ab"
prefix: "b"
suffix: "a"
Returns: "b"