From f8243bfdb3aa468b32067e52e656df8571d26a7d Mon Sep 17 00:00:00 2001 From: Eatswap Date: Sat, 1 Apr 2023 15:48:12 +0800 Subject: [PATCH] add: 230401-CN --- cpp/2304/230401-CN.cpp | 66 +++++++++++++++++++++++++++++++++++++++++ cpp/2304/CMakeLists.txt | 4 +-- 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 cpp/2304/230401-CN.cpp diff --git a/cpp/2304/230401-CN.cpp b/cpp/2304/230401-CN.cpp new file mode 100644 index 0000000..6f256ec --- /dev/null +++ b/cpp/2304/230401-CN.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include + +/** + * 831. Masking Personal Information + * + * You are given a personal information string s, representing either an email address or a phone number. Return the masked personal information using the below rules. + * + * Email address: + * + * An email address is: + * + * A name consisting of uppercase and lowercase English letters, followed by + * The '@' symbol, followed by + * The domain consisting of uppercase and lowercase English letters with a dot '.' somewhere in the middle (not the first or last character). + * To mask an email: + * + * The uppercase letters in the name and domain must be converted to lowercase letters. + * The middle letters of the name (i.e., all but the first and last letters) must be replaced by 5 asterisks "*****". + * Phone number: + * + * A phone number is formatted as follows: + * + * The phone number contains 10-13 digits. + * The last 10 digits make up the local number. + * The remaining 0-3 digits, in the beginning, make up the country code. + * Separation characters from the set {'+', '-', '(', ')', ' '} separate the above digits in some way. + * To mask a phone number: + * + * Remove all separation characters. + * The masked phone number should have the form: + * "***-***-XXXX" if the country code has 0 digits. + * "+*-***-***-XXXX" if the country code has 1 digit. + * "+**-***-***-XXXX" if the country code has 2 digits. + * "+***-***-***-XXXX" if the country code has 3 digits. + * "XXXX" is the last 4 digits of the local number. + */ + +class Solution { +private: + static auto mail(const std::string& s) { + auto pos = s.find('@'); + std::string ret; + ret += std::tolower(s[0]); + ret += "*****"; + ret += std::tolower(s[pos - 1]); + ret += '@'; + for (int i = pos + 1; i < s.length(); ++i) + ret += std::tolower(s[i]); + return ret; + } + + static auto phone(const std::string& s) { + auto cnt = std::count_if(s.begin(), s.end(), [&](auto&& x) { return std::isdigit(x); }); + std::string t; + std::copy_if(s.begin(), s.end(), std::back_inserter(t), [](auto&& x) { return std::isdigit(x); }); + return (!(cnt - 10) ? std::string() : "+" + std::string(cnt - 10, '*') + '-') + "***-***-" + t.substr(t.length() - 4, 4); + } + +public: + static auto maskPII(const std::string& s) { + return ((s.find('@') != -1) ? mail : phone)(s); + } +}; diff --git a/cpp/2304/CMakeLists.txt b/cpp/2304/CMakeLists.txt index 437204d..3e82c5f 100644 --- a/cpp/2304/CMakeLists.txt +++ b/cpp/2304/CMakeLists.txt @@ -1,6 +1,6 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.24) PROJECT(2304) -SET(CMAKE_CXX_STANDARD 23) +SET(CMAKE_CXX_STANDARD 17) -ADD_EXECUTABLE(2304 230401.cpp) +ADD_EXECUTABLE(2304 230401-CN.cpp)