-
[Leetcode] 929. Unique Email Addresses 문제 풀이Algorithm/문제풀이 2021. 9. 27. 20:27
https://leetcode.com/problems/unique-email-addresses/
Unique Email Addresses - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제 내용
이메일에 문자가 섞여있는데 문자를 규칙에 따라 제거했을 때 남은 이메일은 몇개인지 찾는 문제
Example 1:
Input: emails = ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation: "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails.
문제 풀이
unique한 이름을 찾기 위해 중복이 안되는 container인 set를 이용해서 풀었다.
그리고 규칙에 따라 이름을 찾고 갯수를 출력했다.
class Solution { public: string uniqueEmail(string& email) { int at = 0; for (; at < email.length(); ++at) { if (email[at] == '@') break; } string local_name = ""; for (int i = 0; i < at; ++i) { if (email[i] == '.') continue; if (email[i] == '+') break; local_name += email[i]; } return local_name + email.substr(at); } int numUniqueEmails(vector<string>& emails) { set<string> unique; for (auto& email : emails) { unique.insert(uniqueEmail(email)); } return unique.size(); } };
'Algorithm > 문제풀이' 카테고리의 다른 글
[Leetcode] 213. House Robber II 문제 풀이 (0) 2021.09.29 [Leetcode] 922. Sort Array By Parity II 문제 풀이 (0) 2021.09.28 [Leetcode] Shortest Path in a Grid with Obstacles Elimination 문제 풀이 (0) 2021.09.25 [Leetcode] 1137. N-th Tribonacci Number 문제 풀이 (0) 2021.09.24 [Leetcode] 572. Subtree of Another Tree 문제 풀이 (0) 2021.09.22