{"id":5110,"date":"2019-04-24T21:52:06","date_gmt":"2019-04-25T04:52:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5110"},"modified":"2019-04-24T21:54:06","modified_gmt":"2019-04-25T04:54:06","slug":"leetcode-831-masking-personal-information","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-831-masking-personal-information\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 831. Masking Personal Information"},"content":{"rendered":"\n<p>We are given a&nbsp;personal information string&nbsp;<code>S<\/code>, which may represent&nbsp;either&nbsp;<strong>an email address<\/strong>&nbsp;or&nbsp;<strong>a phone number.<\/strong><\/p>\n\n\n\n<p>We would like to mask this&nbsp;personal information according to the&nbsp;following rules:<\/p>\n\n\n\n<p><br><strong>1. Email address:<\/strong><\/p>\n\n\n\n<p>We define a&nbsp;<strong>name<\/strong>&nbsp;to be a string of&nbsp;<code>length \u2265 2<\/code>&nbsp;consisting&nbsp;of only lowercase letters&nbsp;<code>a-z<\/code>&nbsp;or uppercase&nbsp;letters&nbsp;<code>A-Z<\/code>.<\/p>\n\n\n\n<p>An email address starts with a name, followed by the&nbsp;symbol&nbsp;<code>'@'<\/code>, followed by a name, followed by the&nbsp;dot&nbsp;<code>'.'<\/code>&nbsp;and&nbsp;followed by a name.&nbsp;<\/p>\n\n\n\n<p>All email addresses are&nbsp;guaranteed to be valid and in the format of&nbsp;<code>\"name1@name2.name3\".<\/code><\/p>\n\n\n\n<p>To mask an email,&nbsp;<strong>all names must be converted to lowercase<\/strong>&nbsp;and&nbsp;<strong>all letters between the first and last letter of the first name<\/strong>&nbsp;must be replaced by 5 asterisks&nbsp;<code>'*'<\/code>.<\/p>\n\n\n\n<p><br><strong>2. Phone number:<\/strong><\/p>\n\n\n\n<p>A phone number is a string consisting of&nbsp;only the digits&nbsp;<code>0-9<\/code>&nbsp;or the characters from the set&nbsp;<code>{'+', '-', '(', ')', '&nbsp;'}.<\/code>&nbsp;You may assume a phone&nbsp;number contains&nbsp;10 to 13 digits.<\/p>\n\n\n\n<p>The last 10 digits make up the local&nbsp;number, while the digits before those make up the country code. Note that&nbsp;the country code is optional. We want to expose only the last 4 digits&nbsp;and mask all other&nbsp;digits.<\/p>\n\n\n\n<p>The local&nbsp;number&nbsp;should be formatted and masked as&nbsp;<code>\"***-***-1111\",&nbsp;<\/code>where&nbsp;<code>1<\/code>&nbsp;represents the exposed digits.<\/p>\n\n\n\n<p>To mask a phone number with country code like&nbsp;<code>\"+111 111 111 1111\"<\/code>, we write it in the form&nbsp;<code>\"+***-***-***-1111\".<\/code>&nbsp; The&nbsp;<code>'+'<\/code>&nbsp;sign and the first&nbsp;<code>'-'<\/code>&nbsp;sign before the local number should only exist if there is a country code.&nbsp; For example, a 12 digit phone number mask&nbsp;should start&nbsp;with&nbsp;<code>\"+**-\"<\/code>.<\/p>\n\n\n\n<p>Note that extraneous characters like&nbsp;<code>\"(\", \")\", \" \"<\/code>, as well as&nbsp;extra dashes or plus signs not part of the above formatting scheme should be removed.<\/p>\n\n\n\n<p>Return the correct &#8220;mask&#8221; of the information provided.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input: <\/strong>\"LeetCode@LeetCode.com\"\n<strong>Output: <\/strong>\"l*****e@leetcode.com\"\n<strong>Explanation:&nbsp;<\/strong>All names are converted to lowercase, and the letters between the\n&nbsp;            first and last letter of the first name is replaced by 5 asterisks.\n&nbsp;            Therefore, \"leetcode\" -&gt; \"l*****e\".\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input: <\/strong>\"AB@qq.com\"\n<strong>Output: <\/strong>\"a*****b@qq.com\"\n<strong>Explanation:&nbsp;<\/strong>There must be 5 asterisks between the first and last letter \n&nbsp;            of the first name \"ab\". Therefore, \"ab\" -&gt; \"a*****b\".\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input: <\/strong>\"1(234)567-890\"\n<strong>Output: <\/strong>\"***-***-7890\"\n<strong>Explanation:<\/strong>&nbsp;10 digits in the phone number, which means all digits make up the local number.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input: <\/strong>\"86-(10)12345678\"\n<strong>Output: <\/strong>\"+**-***-***-5678\"\n<strong>Explanation:<\/strong>&nbsp;12 digits, 2 digits for country code and 10 digits for local number. \n<\/pre>\n\n\n\n<p><strong>Notes:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>S.length&nbsp;&lt;=&nbsp;40<\/code>.<\/li><li>Emails have length at least 8.<\/li><li>Phone numbers have length at least 10.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: String<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua, running time: 4 ms, 8.4 MB\nclass Solution {\npublic:\n  string maskPII(string S) {    \n    return isalpha(S[0]) ? maskEmail(S) : maskPhone(S);\n  }\nprivate:\n  string maskEmail(const string& s) {    \n    string email = s.substr(0, 1) + \"*****\" + s.substr(s.find('@') - 1);\n    transform(begin(email), end(email), begin(email), ::tolower);\n    return email;\n  }\n  \n  string maskPhone(const string& s) {\n    string d;\n    for (char c : s) if (isdigit(c)) d += c;\n    string phone; \n    if (d.length() > 10)\n      phone += \"+\" + string(d.length() - 10, '*') + \"-\";\n    phone += \"***-***-\" + d.substr(d.length() - 4);\n    return phone;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We are given a&nbsp;personal information string&nbsp;S, which may represent&nbsp;either&nbsp;an email address&nbsp;or&nbsp;a phone number. We would like to mask this&nbsp;personal information according to the&nbsp;following rules: 1.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[426,4],"class_list":["post-5110","post","type-post","status-publish","format-standard","hentry","category-string","tag-email","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5110"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5110\/revisions"}],"predecessor-version":[{"id":5113,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5110\/revisions\/5113"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}