> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Masking Personal Information Python Solution

> Tested Python solution for LeetCode 831 with 23 pytest cases. Generate a practice environment with lcpy.

LeetCode 831, [Medium](/catalog/medium). Topics: [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/masking-personal-information/description/).

Generate this problem as a practice environment: tested reference solution, 23 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 831   # by problem number
lcpy gen -s masking_personal_information   # by problem name
```

## Problem

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*.

\<u>**Email address:**\</u>

An email address is:

* A **name** consisting of **at least** two 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 `"*****"`.

\<u>**Phone number:**\</u>

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**.

### Examples

```
Input: s = "LeetCode@LeetCode.com"
Output: "l*****e@leetcode.com"
Explanation: s is an email address.
The name and domain are converted to lowercase, and the middle of the name is replaced by 5 asterisks.
```

```
Input: s = "AB@qq.com"
Output: "a*****b@qq.com"
Explanation: s is an email address.
The name and domain are converted to lowercase, and the middle of the name is replaced by 5 asterisks.
Note that even though "ab" is 2 characters, it still must have 5 asterisks in the middle.
```

```
Input: s = "1(234)567-890"
Output: "***-***-7890"
Explanation: s is a phone number.
There are 10 digits, so the local number is 10 digits and the country code is 0 digits.
Thus, the resulting masked number is "***-***-7890".
```

### Constraints

* s is either a valid email or a phone number.
* If s is an email:
  * 8 \<= s.length \<= 40
  * s consists of uppercase and lowercase English letters and exactly one '@' symbol and '.' symbol.
* If s is a phone number:
  * 10 \<= s.length \<= 20
  * s consists of digits, spaces, and the symbols '(', ')', '-', and '+'.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/masking_personal_information/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/masking_personal_information/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def mask_pii(self, s: str) -> str:
        if "@" in s:
            name, domain = s.split("@")
            lower_name = name.lower()
            return f"{lower_name[0]}*****{lower_name[-1]}@{domain.lower()}"
        digits = [c for c in s if c.isdigit()]
        country = len(digits) - 10
        tail = "".join(digits[-4:])
        prefix = "+" + "*" * country if country else ""
        return f"{prefix}-***-***-{tail}" if prefix else f"***-***-{tail}"
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.