Shortest Substring with All Characters

Determine the shortest substring of the string S that includes every character from S at least once. If multiple shortest substrings exist, return the one that starts the earliest in S.

Input:

The input contains a single string 'S' consisting of only lowercase English letters.

Output:

Output the shortest substring of 'S' that contains all unique characters from 'S'.

Example:

Input:
"abcba"
Output:
"abc"
Explanation:

In the string S = "abcba", the substrings "abc" and "cba" both contain all characters of S. "abc" begins at index 0, earlier than "cba" which begins at index 2. Hence, the answer is "abc".

Constraints:

  • 1 <= N <= 10^5
  • S consists only of lowercase English letters.

Where S denotes the input string and N is its length.

Note: You do not need to print anything; implement the function as per the requirements.

Be the first one to answer
Add answer anonymously...
Codalien Technologies Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter