FastPrepCount Length-Three Windows with Matching Ends

Count Length-Three Windows with Matching Ends

Visa logoVisa● EasyFULLTIMEOA
Learn

Problem statement

You are given a string text.

Count the contiguous substrings of length exactly 3 whose first and last characters are equal. Windows may overlap. Return the count.

Function

countMatchingEndpointTriples(text: String) → int

Examples

Example 1

text = "abcxccc"return = 2

The matching windows are cxc and ccc.

Example 2

text = "aba"return = 1

The only length-three window begins and ends with a.

Example 3

text = "ab"return = 0

A string shorter than three characters contains no eligible window.

Constraints

  • 0 <= text.length <= 2 * 105
  • text contains printable ASCII characters.

More Visa problems

See Visa hiring insights
public int countMatchingEndpointTriples(String text) {
    // Write your code here.
}
text"abcxccc"
expected2
Checking account…