FastPrepShortest Deletion for Distinct Characters
Problems/Shortest Deletion for Distinct Characters

Shortest Deletion for Distinct Characters

Oracle logoOracle● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: An experienced engineer described an onsite with an Oracle cloud infrastructure team. In the second round the task was to cut one contiguous piece out of a lowercase string, glue the two remaining ends together, and make that cut as short as possible so that no letter is left twice; a string with no repeated letter needs no cut. The report gives one small example with its answer.

The report leaves out the function signature and the input limits. Practice choices: the answer is the length of the cut as one integer, the limits are ours, and two of the three examples are ours.

The judged task matches the report at about 95%.

You are given a string s of lowercase English letters. You may remove one contiguous block of characters from s. The part before the block and the part after it are then joined back together. The block may also be empty, which removes nothing.

Return the smallest block length for which no letter appears more than once in the joined result. If no letter repeats in s, the answer is 0.

Function

minDeletionLength(s: String) → int

Examples

Example 1

s = "aabb"return = 2

Delete the middle substring "ab" (indices 1 and 2). The remaining prefix "a" and suffix "b" join into "ab", where no letter repeats. Deleting only one character always leaves a repeated a or b, so 2 is the minimum.

Example 2

s = "cabcbd"return = 2

c and b each appear twice, so at least one of each must be deleted, which takes at least 2 characters. Deleting "bc" at indices 2 and 3 leaves "ca" and "bd", which join into "cabd" with no repeats.

Example 3

s = "xyz"return = 0

No letter repeats, so nothing needs to be deleted.

Constraints

  • 1 <= s.length <= 105
  • s contains only lowercase English letters.

More Oracle problems

  • Maximum Subarray Sum with One DeletionONSITE INTERVIEW · Seen Nov 2026
  • Count Dropped Rate-Limited RequestsONSITE INTERVIEW · Seen Nov 2026
  • Remove Parentheses from a Plus-Minus ExpressionPHONE SCREEN · Seen Nov 2026
All Oracle interview questionsSee Oracle hiring insights
Saved
class Solution {
    public int minDeletionLength(String s) {
        // Write your code here.
    }
}
Checking account…