FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
coding-Interview/SubdomainVisitCount.java at master · arjunmullick/coding-Interview · GitHub
arjunmullick
/
coding-Interview
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
coding-Interview
/
SubdomainVisitCount.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
45 lines (37 loc) · 1.62 KB
Breadcrumbs
coding-Interview
/
SubdomainVisitCount.java
Copy path
File metadata and controls
45 lines (37 loc) · 1.62 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package
com
.
leetcode
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
LinkedList
;
import
java
.
util
.
List
;
/**
Example:
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.
**/
public
class
SubdomainVisitCount
{
//https://leetcode.com/problems/subdomain-visit-count/
class
Solution
{
public
List
<
String
>
subdomainVisits
(
String
[]
cpdomains
) {
HashMap
<
String
,
Integer
>
map
=
new
HashMap
<>();
for
(
String
domain
:
cpdomains
){
String
[]
countToDomain
=
domain
.
split
(
" "
);
int
count
=
Integer
.
valueOf
(
countToDomain
[
0
]);
String
weburl
=
countToDomain
[
1
];
map
.
put
(
weburl
,
map
.
getOrDefault
(
weburl
,
0
)+
count
);
for
(
int
i
=
0
;
i
<
weburl
.
length
() ;
i
++){
if
(
weburl
.
charAt
(
i
) ==
'.'
){
String
key
=
weburl
.
substring
(
i
+
1
,
weburl
.
length
());
map
.
put
(
key
,
map
.
getOrDefault
(
key
,
0
)+
count
);
}
}
}
//System.out.println(map);
List
<
String
>
result
=
new
LinkedList
<>();
for
(
String
key
:
map
.
keySet
()){
result
.
add
(
map
.
get
(
key
) +
" "
+
key
);
}
return
result
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL