# """# This is HtmlParser's API interface.# You should not implement it, or speculate about its implementation# """# class HtmlParser(object):# def getUrls(self, url):# """# :type url: str# :rtype List[str]# """classSolution:defcrawl(self,startUrl:str,htmlParser:'HtmlParser')->List[str]:defhost(url):url=url[7:]returnurl.split('/')[0]defdfs(url):ifurlinans:returnans.add(url)fornextinhtmlParser.getUrls(url):ifhost(url)==host(next):dfs(next)ans=set()dfs(startUrl)returnlist(ans)
/** * // This is the HtmlParser's API interface. * // You should not implement it, or speculate about its implementation * interface HtmlParser { * public List<String> getUrls(String url) {} * } */classSolution{privateSet<String>ans;publicList<String>crawl(StringstartUrl,HtmlParserhtmlParser){ans=newHashSet<>();dfs(startUrl,htmlParser);returnnewArrayList<>(ans);}privatevoiddfs(Stringurl,HtmlParserhtmlParser){if(ans.contains(url)){return;}ans.add(url);for(Stringnext:htmlParser.getUrls(url)){if(host(next).equals(host(url))){dfs(next,htmlParser);}}}privateStringhost(Stringurl){url=url.substring(7);returnurl.split("/")[0];}}
/** * // This is the HtmlParser's API interface. * // You should not implement it, or speculate about its implementation * class HtmlParser { * public: * vector<string> getUrls(string url); * }; */classSolution{public:vector<string>ans;unordered_set<string>vis;vector<string>crawl(stringstartUrl,HtmlParserhtmlParser){dfs(startUrl,htmlParser);returnans;}voiddfs(string&url,HtmlParser&htmlParser){if(vis.count(url))return;vis.insert(url);ans.push_back(url);for(stringnext:htmlParser.getUrls(url))if(host(url)==host(next))dfs(next,htmlParser);}stringhost(stringurl){inti=7;stringres;for(;i<url.size();++i){if(url[i]=='/')break;res+=url[i];}returnres;}};
/** * // This is HtmlParser's API interface. * // You should not implement it, or speculate about its implementation * type HtmlParser struct { * func GetUrls(url string) []string {} * } */funccrawl(startUrlstring,htmlParserHtmlParser)[]string{varans[]stringvis:=make(map[string]bool)vardfsfunc(urlstring)host:=func(urlstring)string{returnstrings.Split(url[7:],"/")[0]}dfs=func(urlstring){ifvis[url]{return}vis[url]=trueans=append(ans,url)for_,next:=rangehtmlParser.GetUrls(url){ifhost(next)==host(url){dfs(next)}}}dfs(startUrl)returnans}