FastPrepLongest Common Prefix Length

Longest Common Prefix Length

Visa logoVisa● EasyFULLTIMEOA
Learn

Problem statement

You are given two strings a and b.

Return the length of their longest common prefix: the longest string that begins at index 0 in both inputs. Character comparisons are case-sensitive.

Function

longestCommonPrefixLength(a: String, b: String) → int

Examples

Example 1

a = "flower"b = "flow"return = 4

Both strings begin with flow, and the second string ends after four characters.

Example 2

a = "abc"b = "xyz"return = 0

The first characters differ, so the common prefix is empty.

Constraints

  • 0 <= a.length, b.length <= 105
  • The combined input length is at most 2 * 105.
  • Both strings contain printable ASCII characters.

More Visa problems

See Visa hiring insights
public int longestCommonPrefixLength(String a, String b) {
    // Write your code here.
}
a"flower"
b"flow"
expected4
Checking account…