FastPrepGenerate Character N-Grams from a URL

Generate Character N-Grams from a URL

Microsoft logoMicrosoft● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Given a URL string url and a positive integer n, return every overlapping character n-gram in left-to-right order.

Use the URL exactly as supplied: matching is case-sensitive, no decoding or normalization is performed, and repeated n-grams are retained. If n is greater than the URL length, return an empty array.

Function

urlNGrams(url: String, n: int) → String[]

Examples

Example 1

url = "abc"n = 2return = ["ab","bc"]

The two overlapping length-two windows begin at indexes 0 and 1.

Example 2

url = "aaaa"n = 2return = ["aa","aa","aa"]

Each overlapping occurrence is retained, even when the n-gram text repeats.

Constraints

  • 0 <= url.length <= 100000
  • 1 <= n <= 100000
  • url contains printable ASCII characters.

More Microsoft problems

See Microsoft hiring insights
public String[] urlNGrams(String url, int n) {
  // Write your code here.
}
url"abc"
n2
expected["ab", "bc"]
Checking account…