Problem 346315 · easy · Phase 03 Linear Management & Searching

Shared Prefix Ignoring Hyphens

two pointers · strings

Two product codes are compared ignoring hyphens (-). Given strings a and b, return their longest common prefix with hyphens removed, as a string.

Examples

Input:  a = "data-base", b = "da-tab"
Output: "datab"
Explanation: without hyphens they read "database" and "datab".

Input:  a = "x-y-z", b = "xz"
Output: "x"

Constraints

  • 0 <= len(a), len(b) <= 10**5
  • Target: O(len(a) + len(b)) time; do not build hyphen-free copies of the inputs.

Goals

  • Run two independent pointers that each skip unwanted characters
  • Stop at the first real mismatch or when either string ends
Starting Python…