U

434. Two Strings, Find Their Longest Common

Med.

Problem Description

Given two strings, find their longest common prefix.

Input Format

A single line containing string SS.

Output Format

Print the computed string or integer scalar.

Example 1:
Input:
racecar
Output:
true
Explanation: "racecar" reads identically forward and backward.
Example 2:
Input:
hello
Output:
false
Explanation: "hello" is not a palindrome.
Example 3:
Input:
a
Output:
true
Explanation: A single character string is trivially symmetric.
Constraints:
  • 1 <= length(S) <= 10^5
  • Characters consist of printable ASCII
  • Time complexity target: O(N)
  • Auxiliary space target: O(1) or O(N)
Follow up:Can this algorithm be adapted to an online streaming setting where the entire input cannot be held in memory simultaneously?
1,180 Online