Interview Questions/Coding/Best Boundary Match Substring

Best Boundary Match Substring

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Best Boundary Match Substring

You are given three strings: text, prefixString, and suffixString. Choose a non-empty substring of text. Its prefix score is the largest length k such that the substring starts with the last k characters of prefixString. Its suffix score is the largest length k such that the substring ends with the first k characters of suffixString. The total score is prefix score plus suffix score. Return the substring with the highest total score. If multiple substrings have the same score, return the lexicographically smallest one.

Examples

Example 1
Input:
nothing
bruno
ingenious
Output:
nothing

Explanation: nothing starts with n, matching the end of bruno, and ends with ing, matching the beginning of ingenious. Its total score is 5.

Approach hint

For every candidate substring, compare its start with suffixes of prefixString.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp