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.


&nbsp;
Example 1:

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.


Example 2:

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.


Example 3:

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


&nbsp;
Constraints:


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


class Solution:
    def maskPII(self, s: str) -> str:
        res=''
        if '@' in s:
            
            l=s.split('@')
            
            a=l[0]
            b=l[1]
            
            c=b.split('.')
            
            
            res=a[0].lower()+'*'*5+a[-1].lower()+'@'+c[0].lower()+'.'+c[1].lower()
            return res
        else:
            l=0
            res=''
            flag=False
            f=False
            c=0
            for i in range(len(s)-1,-1,-1):
                if s[i]=="(" or s[i]==')' or s[i]=='-' or s[i]=='+' or s[i]==' ':
                    continue
                  
                if f==True:
                    c+=1
                    continue
                
                if flag==False:
                    res=s[i]+res
                else:
                    res='*'+res
                    
                
                
                if l==3 or l==6:
                    if l==3:
                        flag=True
                    res='-'+res
                l+=1
                
                if len(res)==12:
                    f=True
            if c==1:
                res='+*-'+res
            elif c==2:
                res="+**-"+res
            elif c==3:
                res="+***-"+res
                    
           
            return res
            
                    
                    
                    
                    
        
class Solution {
    public String maskPII(String s) {
        StringBuilder sb = new StringBuilder();
                 //email handeling
        if((s.charAt(0) >= 97 && s.charAt(0) <= 122) || (s.charAt(0) >= 65 && s.charAt(0) <= 90)){

            s = s.toLowerCase();
           int indexofAt = s.indexOf('@');
           String firstName = s.substring(0, indexofAt);
           sb.append(firstName.charAt(0)).append("*****").append(firstName.charAt(firstName.length()-1));
           sb.append(s.substring(indexofAt,s.length()));
        }
        //phone number handeling
        else{
           int digits = 0;
           for(int i = 0 ; i < s.length(); i++){
               if(Character.isDigit(s.charAt(i))){
                   digits++;
               }
           }
           if(digits > 10){
               sb.append('+');
           }
           while(digits > 10){
               sb.append('*');
               digits--;
           }
           if(sb.toString().isEmpty() == false){
               sb.append('-');
           }
           sb.append("***").append('-').append("***-");
           StringBuilder last4 = new StringBuilder();
           int count = 0;
           for(int i = s.length()-1; i >=0; --i){
               if(count == 4){
                   break;
               }
               if(Character.isDigit(s.charAt(i))){
                   last4.append(s.charAt(i));
                   count++;
               }
           }
           sb.append(last4.reverse());
        }

        return sb.toString();
    }
}
class Solution {
public:
  string email(string &s, int id){
    string t = string(1, s[0] | ' ') + "*****" + string(1, s[id-1] | ' ');
  
    for(int i = id + 1; i != s.size(); i++) 
      if(s[i] != '.') s[i] |= ' ';
    
    return t + s.substr(id, s.size());
  }
  
  string phone(string &s){
    string table[] = { "" , "+*-", "+**-", "+***-" }, t;
    
    for(auto &ch: s)
      if(ch >= '0' && ch <= '9') t.push_back(ch);
   
    return table[t.size() - 10] + "***-***-" + t.substr(t.size() - 4, 4);
  }
  
  string maskPII(string s) {
    int id = s.find('@'); 
    return id == -1 ? phone(s) : email(s, id);
  }
};
var maskPII = function(S) {
    if (isEmail(S)) return maskEmail(S);
    return maskPhone(S);
    
    function isEmail(str) {
       return /^[A-Za-z]{2,}@[A-Za-z]{2,}.[A-Za-z]{2,}$/.test(str);
    }
    
    function maskEmail(str) {
        let res = "";
        const [local, domain] = str.split("@");
        
        res += local.charAt(0).toLowerCase() + "*****" + local.charAt(local.length - 1).toLowerCase();
        return res + "@" + domain.toLowerCase(); 
    }
    
    function maskPhone(str) {
        const onlyDigits = str.replace(/\D/g, "");
        
        const localNum = "***-***-" + onlyDigits.substring(onlyDigits.length - 4);
        
        if (onlyDigits.length === 10) return localNum;
        
        let countryNum = "+" + "*".repeat(onlyDigits.length - 10) + "-";
        
        return countryNum + localNum;
    } 
};
Masking Personal Information