FastPrepCase-Insensitive Target Distances

Case-Insensitive Target Distances

Google logoGoogle● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

For every character position in s, return the smallest absolute index distance to any occurrence of target. Match uppercase and lowercase ASCII letters as the same letter. A target position has distance zero.

If the target does not occur, return -1 for every position. An empty string returns an empty array.

Function

shortestDistanceToTarget(s: String, target: String) → int[]

Examples

Example 1

s = "Google"target = "g"return = [0,1,1,0,1,2]

The uppercase G and lowercase g are both target occurrences, at indices 0 and 3.

Example 2

s = "abAba"target = "a"return = [0,1,0,1,0]

Indices 0, 2 and 4 match, so each intervening character is one step away.

Example 3

s = "xyz"target = "a"return = [-1,-1,-1]

No position contains the target letter.

Constraints

  • 0 <= s.length <= 100000.
  • s contains only ASCII letters.
  • target contains exactly one ASCII letter.

More Google problems

See Google hiring insights
public int[] shortestDistanceToTarget(String s, String target) {
    // Write your code here
}
s"Google"
target"g"
expected[0,1,1,0,1,2]
Checking account…