Skip to the code
cf.
+10 XP

#615 Prefix That Echoes at the End

easy

A chat app notices when a message ends the same way it began, like ok, I said ok, to suggest a shorter version.

Write echo(message) that returns the longest string that is both a prefix and a suffix of message, but is not the whole message. Return an empty string if there is none.

Example 1

Input: message = "ok, I said ok"
Output: "ok"

It starts with ok and ends with ok.

Example 2

Input: message = "abab"
Output: "ab"

ab is both the start and the end; abab itself doesn't count.

Constraints

  • 1 <= message.length <= 10^5
  • message contains printable ASCII characters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).