Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Remove Duplicates From A String

Easy Acceptance 61.68% Points 20.00

Given a string str that may contain both lowercase and uppercase letters, your task is to remove all duplicate characters from str while preserving the order of their first occurrence.

Examples
Example 1

image Input: "HappyProgramming"

Output: "HapyProgrmin"

Explanation: The resultant string is formed by retaining the first occurrence of each character.

Example 2

image Input: "aAaAaA"

Output: "aA"

Explanation: The first occurrence of a and A is preserved, and the duplicates are removed.

Hints
Hint 1
Expected Time Complexity: O(N)
Hint 2
Expected Space Complexity: O(N)
Constraints
  • 1 <= N <= 10^5
  • String contains uppercase and lowercase English letters.
Companies
Amazon Microsoft Samsung Adobe TCS + 1 more
Topics
String Stack
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions