Problem · String

Minimum No-Repeat Segments After One Character Removal

Learn this problem
MediumSalesforce logoSalesforceFULLTIMEOA
See Salesforce hiring insights

Problem statement

Given a lowercase English string s, choose exactly one lowercase English character and remove every occurrence of that character from s.

Partition the remaining string into the minimum number of non-empty, disjoint, contiguous segments such that no segment contains a repeated character. Return that minimum number of segments.

If removing the chosen character makes the string empty, return 0.

Function

getNoRepeatSegments(s: String) → int

Examples

Example 1

s = "abcccde"return = 1

Remove every 'c'. The remaining string is "abde", whose characters are all distinct, so one segment is sufficient.

Example 2

s = "abdaa"return = 1

Remove every 'a'. The remaining string is "bd", so the minimum is one segment.

Example 3

s = "aaaa"return = 0

Remove every 'a'. The remaining string is empty, so there are no non-empty segments.

Constraints

  • 1 <= s.length <= 2 * 10^5
  • s contains only lowercase English letters.

More Salesforce problems

drafts saved locally
public int getNoRepeatSegments(String s) {
    // write your code here
}
s"abcccde"
expected1
checking account