leetcode

929. Unique Email Addresses

rycbar2592 2020. 8. 21. 00:21

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

시간 복잡도: O(N)

조건 1)    '+'를 만나면 @를 만날 때 까지 문자를 무시할 수 있다.

조건 2)    '@'뒤에 있는 .은 생략하면 안된다.

조건 3)    '@'앞에 있는 .은 포함하면 안된다.(afterDot변수로 구분지음)

조건 4)    중복되는 문자열은 제거해야 한다.

#include<iostream>
#include<string>
#include<set>
#include<vector>
using namespace std;

class Solution {
public:
    string uniqueEmail(string str){
        string answer = "";
        bool afterDot = false;
        for(int i = 0; i < str.length(); i++){
            if(str[i] == '+'){
                while(1) {      //+를 만나면 @를 만날 때까지 넘어간다.
                    if(str[i] == '@'){
                        answer += '@';
                        afterDot = true;
                        break;
                    }
                    i++;
                }
            } else if(str[i] == '@'){   //+를 안만나고 @를 만났을 때
                afterDot = true;
                answer += '@';
            } else if(str[i] == '.') {
                if(afterDot) answer += str[i];  //@이후 .은 저장하고 이전 .은 저장하지 않는다.
            } else answer += str[i];
        }
        return answer;
    }

    int numUniqueEmails(vector<string>& emails) {
        set<string> s;
        for(int i = 0; i < emails.size(); i++)
            s.insert(uniqueEmail(emails[i]));
        return s.size();
    }
};

int main(void){
  return 0;
}