A website domain "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com"&nbsp;and at the lowest level, "discuss.leetcode.com". When we visit a domain like "discuss.leetcode.com", we will also visit the parent domains "leetcode.com" and "com" implicitly.

A count-paired domain is a domain that has one of the two formats "rep d1.d2.d3" or "rep d1.d2" where rep is the number of visits to the domain and d1.d2.d3 is the domain itself.


	For example, "9001 discuss.leetcode.com" is a count-paired domain that indicates that discuss.leetcode.com was visited 9001 times.


Given an array of count-paired domains cpdomains, return an array of the count-paired domains of each subdomain in the input. You may return the answer in any order.

&nbsp;
Example 1:

Input: cpdomains = ["9001 discuss.leetcode.com"]
Output: ["9001 leetcode.com","9001 discuss.leetcode.com","9001 com"]
Explanation: We only have one website domain: "discuss.leetcode.com".
As discussed above, the subdomain "leetcode.com" and "com" will also be visited. So they will all be visited 9001 times.


Example 2:

Input: cpdomains = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
Output: ["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
Explanation: We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org" 5 times.
For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times.


&nbsp;
Constraints:


	1 &lt;= cpdomain.length &lt;= 100
	1 &lt;= cpdomain[i].length &lt;= 100
	cpdomain[i] follows either the "repi d1i.d2i.d3i" format or the "repi d1i.d2i" format.
	repi is an integer in the range [1, 104].
	d1i, d2i, and d3i consist of lowercase English letters.


class Solution:
    def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
        output, ans = {}, []
        for domain in cpdomains : 
            number, domain = domain.split(' ')
            sub_domain = domain.split('.')
            pair = ''
            print(sub_domain)
            for i in reversed(range(len(sub_domain))) :
                if i == len(sub_domain)-1 : 
                    pair += sub_domain[i]
                else : 
                    pair = sub_domain[i] +'.'+ pair 
                print(pair)
                
                # output.append(str(number) + ' '+pair)
                if pair not in output.keys() : 
                    output[pair] = int(number)
                else : 
                    output[pair] += int(number)
                    
        for key in output.keys() : 
            ans.append(str(output[key]) + ' '+key)
                
        return ans
class Solution {
    public List<String> subdomainVisits(String[] cpdomains) {
        List<String> result = new LinkedList<>();
        HashMap<String, Integer> hmap = new HashMap<>();
        
        for(int i = 0; i < cpdomains.length; i++){
            String[] stringData = cpdomains[i].split(" ");
            String[] str = stringData[1].split("\\.");
            String subDomains = "";
            
            for(int j = str.length-1; j >= 0; j--){
                subDomains = str[j] + subDomains;
                    
                if(!hmap.containsKey(subDomains))
                    hmap.put(subDomains, Integer.parseInt(stringData[0]));
                else
                    hmap.put(subDomains, hmap.get(subDomains) + Integer.parseInt(stringData[0]));
                subDomains = "." + subDomains;
            }
            
        }
        
        for(Map.Entry<String, Integer> entry: hmap.entrySet()){
            result.add(entry.getValue() + " " + entry.getKey());
        }
        
        return result;
    }
}
class Solution {
public:
    vector<string> subdomainVisits(vector<string>& cpdomains) {
        vector<string> v;
        unordered_map<string,int> m;
        for(auto it : cpdomains){
            string rep="";
            int i=0;
            while(it[i]!=' '){
                  rep+=(it[i]);
                    i++;
            }
            int r=stoi(rep);
            m[it.substr(i+1,it.size())]+=r;

            while(it[i]!='.'){
                i++;
            }
            m[it.substr(i+1,it.size())]+=r;
            i++;
            while(i<it.size() and it[i]!='.'){
                i++;
            }
            if(i!=it.size()){
            m[it.substr(i+1,it.size())]+=r;
            }

        }

        for(auto it : m){
            string a=it.first;
            string b=to_string(it.second);
            a=b+" "+a;
            v.push_back(a);
        }
        return v;
    }
};
/**
 * @param {string[]} cpdomains
 * @return {string[]}
 */
var subdomainVisits = function(cpdomains) {
    let map = {}
    cpdomains.forEach(d => {
        let data = d.split(" ");
        let arr = data[1].split(".")
        while(arr.length > 0) {
            if(arr.join(".") in map) map[arr.join(".")]+=+data[0]
            else map[arr.join(".")] = +data[0]
            arr.shift()
        }
    })
    let result = []
    for(let key in map) result.push(map[key] + " "+ key)
    return result
};
Subdomain Visit Count