Case-Insensitive Target Distances
Updated
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.scontains only ASCII letters.targetcontains exactly one ASCII letter.
More Google Phone Screen questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
- Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
- Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Largest Number Below N from Reusable DigitsByteDance · PHONE SCREENByteDance interview questions
- Merge Weekday and Time IntervalsNextdoor · PHONE SCREENNextdoor interview questions
- Query Words Within K PositionsPalantir · PHONE SCREENPalantir interview questions
- Group AnagramsWorkday · PHONE SCREEN
- Apply Operations to Make a String EmptyFlexport · PHONE SCREENFlexport interview questions
- Elevator Scheduling SimulationOpenAI · PHONE SCREENOpenAI interview questions
- Word BreakAmazon · PHONE SCREEN · ONSITE INTERVIEW