Skip to content

Commit 882996c

Browse files
Added Leet Code 1242 solution using stream for each.
1 parent 4d085cb commit 882996c

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Happy to accept any contributions to this in any langugage.
4848
13. [Leet Code 70 Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Solution DP Top Down](./level_easy/LeetCode_70_Climbing_Stairs_Easy.ts) | [Solution DP Bottoms Up](level_easy/LeetCode_70_Climbing_Stairs_1_Easy.ts)
4949
14. [Leet Code 200 Number of Islands](https://leetcode.com/problems/number-of-islands/) | [Solution 1 DFS](./level_medium/LeetCode_200_NumberOfIslands.ts) | [Solution BFS](./level_medium/LeetCode_200_NumberOfIslands_1.ts) | [Solution Union Find](./level_medium/LeetCode_200_NumberOfIslands_2.ts)
5050
15. [Leet Code 362 Design Hit Counter](https://leetcode.com/problems/design-hit-counter/) | [Solution 1](./level_medium/LeetCode_362_Hit_Counter_1.ts) | [Solution 2](./level_medium/LeetCode_362_Hit_Counter_Medium.ts)
51-
16. [LeetCode Web Crawler 1242](https://leetcode.com/problems/web-crawler-multithreaded/) | [Solution 1 ](./level_medium/LeetCode_WebCrawler_MultiThreaded_1242.java)
51+
16. [LeetCode Web Crawler 1242](https://leetcode.com/problems/web-crawler-multithreaded/) | [Solution 1 ](./level_medium/LeetCode_WebCrawler_MultiThreaded_1242.java) | [Solution 2](level_medium/LeetCode_WebCrawler_MultiThreaded_1242_1.java)
5252

5353

5454
## Hard
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* // This is the HtmlParser's API interface.
3+
* // You should not implement it, or speculate about its implementation
4+
* interface HtmlParser {
5+
* public List<String> getUrls(String url) {}
6+
* }
7+
*/
8+
class Solution {
9+
/**
10+
* Solution uses the approach for the DFS. However, rather than creating an immutable function and collecting all the elements that have
11+
* been processed and returned successfully from the recursive calls, we keep on appending them to the final set.
12+
*
13+
* This set is finally returned as a list.
14+
*/
15+
public List<String> crawl(String startUrl, HtmlParser htmlParser) {
16+
final String hostName = this.getHostName(startUrl);
17+
final Set<String> visitedUrls = Collections.synchronizedSet(new HashSet());
18+
this.crawlUrl(startUrl, htmlParser, hostName, visitedUrls);
19+
return new ArrayList(visitedUrls);
20+
}
21+
22+
private void crawlUrl(String startUrl,
23+
HtmlParser htmlParser,
24+
String domainName,
25+
Set<String> visitedUrls) {
26+
27+
visitedUrls.add(startUrl);
28+
htmlParser.getUrls(startUrl)
29+
.parallelStream()
30+
.filter(url -> getHostName(url).equals(domainName))
31+
.filter(url -> !visitedUrls.contains(url))
32+
.forEach(url -> crawlUrl(url, htmlParser, domainName, visitedUrls));
33+
}
34+
35+
private String getHostName(String url) {
36+
final int index = url.indexOf("/", 7); // Finding the indexes for the url post protocol.
37+
return (index == -1) ? url : url.substring(0, index);
38+
}
39+
}

0 commit comments

Comments
 (0)