FastPrepMinimum Unique-Character Segments After Deletion

Minimum Unique-Character Segments After Deletion

Salesforce logoSalesforce● MediumFULLTIMEOA
Learn

Problem statement

You are given a string s containing only lowercase English letters.

Perform exactly one deletion operation:

  1. Choose any lowercase English letter removed. The chosen letter does not need to appear in s.
  2. Delete every occurrence of removed from s.

After the deletion, partition the remaining string into the minimum possible number of contiguous, nonempty segments such that no letter appears more than once inside any segment.

Return the minimum segment count obtainable over all choices of removed. If the deletion leaves an empty string, return 0.

Function

minSegmentsAfterDeletion(s: String) → int

Examples

Example 1

s = "abac"return = 1

Choose 'a'. Removing every 'a' leaves "bc", whose letters are distinct, so one segment is enough.

Example 2

s = "abacbc"return = 2

Deleting 'a' leaves "bcbc", which can be partitioned as "bc" | "bc". Deleting 'c' similarly leaves "abab". No deletion choice makes every remaining letter distinct, so the answer is 2.

Example 3

s = "aaaa"return = 0

Choose 'a'. Every character is deleted, so the remaining empty string needs zero nonempty segments.

Constraints

  • 1 <= s.length <= 2 * 10^5.
  • s contains only lowercase English letters.
  • The deletion operation chooses one of the 26 lowercase English letters and removes all of its occurrences.

More Salesforce problems

See Salesforce hiring insights
public int minSegmentsAfterDeletion(String s) {
    // Write your code here.
}
s"abac"
expected1
Checking account…