FastPrepShortest Deletion for a Distinct Remainder

Shortest Deletion for a Distinct Remainder

Google logoGoogle● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

Given a case-sensitive printable-ASCII string, delete one contiguous substring so that every remaining character is distinct.

Return the minimum number of deleted characters. Deleting zero characters is allowed, and deleting the entire string is also a valid fallback.

Function

minDeletionForDistinct(text: String) → int

Examples

Example 1

text = "abca"return = 1

Deleting either occurrence of a leaves three distinct characters.

Example 2

text = "abc"return = 0

The original string already contains no repeated character.

Constraints

  • 0 <= text.length <= 10^5
  • text contains printable ASCII characters.

More Google problems

See Google hiring insights
public int minDeletionForDistinct(String text) {
    // Write your code here.
}
text"abca"
expected1
Checking account…