[LeetCode]중복 문자 제거

Inhwan98·2023년 3월 30일
0

PTU STUDY_leetcode

목록 보기
20/24

문제

중복된 문자를 제거하고 사전식 순서로 나열하라.

예제

  • Example 1:
Input: s = "bcabc"
Output: "abc"
  • Example 2:
Input: s = "cbacdcbc"
Output: "acdb"

코드

class Solution {
public:
    string removeDuplicateLetters(string s) {

	unordered_map<char, int> chars;
	vector<char> st_v;
	for (char c : s) chars[c] += 1;
	
	for (char a : s)
	{
		chars[a]--;
		if (find(st_v.begin(), st_v.end(), a) != st_v.end())
			continue;

		while (!st_v.empty() && st_v.back() > a && chars[st_v.back()] > 0)
		{
			st_v.pop_back();
		}
		st_v.push_back(a);
	}

	return string(st_v.begin(), st_v.end());
    }
};

결과

Runtime 4 ms / Memory 6.6 MB
https://leetcode.com/problems/remove-duplicate-letters/submissions/925088527/


https://leetcode.com/problems/remove-duplicate-letters/

profile
코딩마스터

0개의 댓글