Problem · String

Shortest Substring

Learn this problem
MediumRipplingINTERNOA

Problem statement

Determine the length of the shortest contiguous substring that can be deleted from a string s so that the remaining string contains only distinct characters.

After deleting a substring, join the part before it with the part after it. If s already contains only distinct characters, return 0.

Function

findShortestSubstring(s: String) → int

Examples

Example 1

s = "abcbbck"return = 3

Deleting any one of three length-3 choices can leave abck, whose characters are all distinct. No shorter deletion works.

Example 2

s = "xabbcacpqr"return = 3

Delete bca to obtain xabcpqr.

Constraints

  • 1 <= s.length() <= 10^5
  • s contains only lowercase English letters.

More Rippling problems

drafts saved locally
public int findShortestSubstring(String s) {
  // write your code here
}
s"abcbbck"
expected3
checking account