<a href="https://careers.kakao.com/index">
์ ํํ๋ฅผ ๊ฐ์ง๋ค.<a>
๋ด์ ๋ค๋ฅธ attribute๊ฐ ์ฃผ์ด์ง๋ ๊ฒฝ์ฐ๋ ์์ผ๋ฉฐ ํญ์ href๋ก ์ฐ๊ฒฐํ ์ฌ์ดํธ์ url๋ง ํฌํจ๋๋ค.https://careers.kakao.com/index
๋ก ์ธ๋ถ๋งํฌ๋ฅผ ๊ฐ์ง๊ณ ์๋ค๊ณ ๋ณผ ์ ์๋ค.word : blind
pages :
[
"<html lang="ko" xml:lang="ko" xmlns="http://www.w3.org/1999/xhtml">\n<head>\n <meta charset="utf-8">\n <meta property="og:url" content="https://a.com"/>\n</head> \n<body>\nBlind Lorem Blind ipsum dolor Blind test sit amet, consectetur adipiscing elit. \n<a href="https://b.com"> Link to b </a>\n</body>\n</html>",
"<html lang="ko" xml:lang="ko" xmlns="http://www.w3.org/1999/xhtml">\n<head>\n <meta charset="utf-8">\n <meta property="og:url" content="https://b.com"/>\n</head> \n<body>\nSuspendisse potenti. Vivamus venenatis tellus non turpis bibendum, \n<a href="https://a.com"> Link to a </a>\nblind sed congue urna varius. Suspendisse feugiat nisl ligula, quis malesuada felis hendrerit ut.\n<a href="https://c.com"> Link to c </a>\n</body>\n</html>",
"<html lang="ko" xml:lang="ko" xmlns="http://www.w3.org/1999/xhtml">\n<head>\n <meta charset="utf-8">\n <meta property="og:url" content="https://c.com"/>\n</head> \n<body>\nUt condimentum urna at felis sodales rutrum. Sed dapibus cursus diam, non interdum nulla tempor nec. Phasellus rutrum enim at orci consectetu blind\n<a href="https://a.com"> Link to a </a>\n</body>\n</html>"
]
pages๋ ๋ค์๊ณผ ๊ฐ์ด 3๊ฐ์ ์นํ์ด์ง์ ํด๋นํ๋ HTML ๋ฌธ์์ด์ด ์์๋๋ก ๋ค์ด์๋ค.
<html lang="ko" xml:lang="ko" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta property="og:url" content="https://a.com"/>
</head>
<body>
Blind Lorem Blind ipsum dolor Blind test sit amet, consectetur adipiscing elit.
<a href="https://b.com"> Link to b </a>
</body>
</html>
<html lang="ko" xml:lang="ko" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta property="og:url" content="https://b.com"/>
</head>
<body>
Suspendisse potenti. Vivamus venenatis tellus non turpis bibendum,
<a href="https://a.com"> Link to a </a>
blind sed congue urna varius. Suspendisse feugiat nisl ligula, quis malesuada felis hendrerit ut.
<a href="https://c.com"> Link to c </a>
</body>
</html>
<html lang="ko" xml:lang="ko" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta property="og:url" content="https://c.com"/>
</head>
<body>
Ut condimentum urna at felis sodales rutrum. Sed dapibus cursus diam, non interdum nulla tempor nec. Phasellus rutrum enim at orci consectetu blind
<a href="https://a.com"> Link to a </a>
</body>
</html>
์์ ์๋ฅผ ๊ฐ์ง๊ณ ๊ฐ๊ฐ์ ์ ์๋ฅผ ๊ณ์ฐํด๋ณด์.
a.com
์ ๊ธฐ๋ณธ์ ์๋ 3, ์ธ๋ถ ๋งํฌ ์๋ 1๊ฐb.com
์ ๊ธฐ๋ณธ์ ์๋ 1, ์ธ๋ถ ๋งํฌ ์๋ 2๊ฐc.com
์ ๊ธฐ๋ณธ์ ์๋ 1, ์ธ๋ถ ๋งํฌ ์๋ 1๊ฐa.com
์ ๋งํฌ์ ์๋ b.com์ผ๋ก๋ถํฐ 0.5์ , c.com์ผ๋ก๋ถํฐ 1์ b.com
์ ๋งํฌ์ ์๋ a.com์ผ๋ก๋ถํฐ 3์ c.com
์ ๋งํฌ์ ์๋ b.com์ผ๋ก๋ถํฐ 0.5์ a.com
: 4.5 ์ b.com
: 4 ์ c.com
: 1.5 ์ ๋ฐ๋ผ์ ๋งค์นญ์ ์๊ฐ ์ ์ผ ๋์ ์ฒซ๋ฒ์งธ ์น ํ์ด์ง์ index์ธ 0์ ๋ฆฌํด ํ๋ฉด ๋๋ค.
word : Muzi
pages :
[
"<html lang="ko" xml:lang="ko" xmlns="http://www.w3.org/1999/xhtml">\n<head>\n <meta charset="utf-8">\n <meta property="og:url" content="https://careers.kakao.com/interview/list"/>\n</head> \n<body>\n<a href="https://programmers.co.kr/learn/courses/4673"></a>#!MuziMuzi!)jayg07con&&\n\n</body>\n</html>",
"<html lang="ko" xml:lang="ko" xmlns="http://www.w3.org/1999/xhtml">\n<head>\n <meta charset="utf-8">\n <meta property="og:url" content="https://www.kakaocorp.com"/>\n</head> \n<body>\ncon%\tmuzI92apeach&2<a href="https://hashcode.co.kr/tos"></a>\n\n\t^\n</body>\n</html>"
]
pages๋ ๋ค์๊ณผ ๊ฐ์ด 2๊ฐ์ ์นํ์ด์ง์ ํด๋นํ๋ HTML ๋ฌธ์์ด์ด ์์๋๋ก ๋ค์ด์๋ค.
<html lang="ko" xml:lang="ko" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta property="og:url" content="https://careers.kakao.com/interview/list"/>
</head>
<body>
<a href="https://programmers.co.kr/learn/courses/4673"></a>#!MuziMuzi!)jayg07con&&
</body>
</html>
<html lang="ko" xml:lang="ko" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta property="og:url" content="https://www.kakaocorp.com"/>
</head>
<body>
con% muzI92apeach&2<a href="https://hashcode.co.kr/tos"></a>
^
</body>
</html>
์์ ์๋ฅผ ๊ฐ์ง๊ณ ๊ฐ๊ฐ์ ์ ์๋ฅผ ๊ณ์ฐํด๋ณด์.
careers.kakao.com/interview/list
์ ๊ธฐ๋ณธ์ ์๋ 0, ์ธ๋ถ ๋งํฌ ์๋ 1๊ฐwww.kakaocorp.com
์ ๊ธฐ๋ณธ์ ์๋ 1, ์ธ๋ถ ๋งํฌ ์๋ 1๊ฐcareers.kakao.com/interview/list
์ ๋งํฌ์ ์๋ 0์ www.kakaocorp.com
์ ๋งํฌ์ ์๋ 0์ careers.kakao.com/interview/list
: 0์ www.kakaocorp.com
: 1 ์ ๋ฐ๋ผ์ ๋งค์นญ์ ์๊ฐ ์ ์ผ ๋์ ๋๋ฒ์งธ ์น ํ์ด์ง์ index์ธ 1์ ๋ฆฌํด ํ๋ฉด ๋๋ค.
ํน๋ณํ ์๊ณ ๋ฆฌ์ฆ์ด ํ์ํ๊ธฐ๋ณด๋จ ๋ฌธ์์ด ํ์ฑ์ ์ ํด์ผํ๋ ๋ฌธ์ ๋ผ ๋ณ ๊ณ ๋ฏผ ์์ด ์ฝ๋๋ฅผ ์์ฑํ์ต๋๋ค.
๋จผ์ ์๋์ ๊ฐ์ด ํ์ด์ง ๊ฐ์ ํ์ฑํ๋ ํจ์๋ฅผ ์์ฑํ์ต๋๋ค.
const parsePage = (page, index, word) => {
//ํ์ด์ง์ ์ฃผ์์ ์ธ๋ถ ๋งํฌ๋ ์ ๊ท์์ ํตํด ์ฐพ์ ์ ์๋ค
const url = page.match(/<meta property="og:url" content="(.+?)"/i)[1];
//ํ์ด์ง์ body ๋ด์ฉ์ ๊ฐ์ ธ์ ๋ฏธ๋ฆฌ ์๋ฌธ์ํํ๋ค
let body = page.match(/<body>([wW]+)</body>/i)[1].toLowerCase();
//replaceAll์ ์ฝ๋ฐฑ ํจ์๋ก ์ธ๋ถ ๋งํฌ๋ฅผ ์์งํ๊ณ body์์ ์ ๊ฑฐ
const externalUrls = [];
body = body.replaceAll(/<a href="(.+?)">/gi, (_, externalUrl) => {
externalUrls.push(externalUrl);
return '';
});
//์๋ค๊ฐ ์๋ฌธ์๊ฐ ์๋๋ฐ, ๋จ์ด์ ์ผ์นํ๋ ๊ฒฝ์ฐ์ ๊ฐฏ์๋ฅผ ๊ธฐ๋ณธ ์ ์๋ก ์ฌ์ฉ
const basePoint = (body.match(new RegExp(`[^a-z]${word}[^a-z]`, 'ig')) || []).length;
//ํ์ฑ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ฒด๋ก ๋ฐํ
return {
index,
url,
externalUrls,
basePoint,
totalPoint: basePoint
};
};
๊ทธ ํ ํ์ด์ง๋ฅผ ํ์ฑํ ๊ฒฐ๊ณผ๋ฅผ Map์ ๋ด์์ต๋๋ค. ์๋ ๋ฐฐ์ด์ ์ฌ์ฉํ๋ ค๊ณ ํ๋๋ฐ, ์ธ๋ถ ๋งํฌ ์ ์๋ฅผ ๊ณ์ฐํ ๋ url์ ํค๋ก ์ฌ์ฉํ๊ธฐ ์ํด Map์ ์ฌ์ฉํ์ต๋๋ค.
๋งคํ์ด ๋๋ ํ์ ์ธ๋ถ ๋งํฌ ์ ์๋ฅผ totalPoint์ ๋ํด ์ด ์ ์๋ฅผ ๊ณ์ฐํ๊ณ , ์ด์ ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌํ ๊ฒฐ๊ณผ์ ์ฒซ๋ฒ์งธ ์์์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํํ์ต๋๋ค.
function solution(word, pages) {
//์
๋ ฅ๋ ๋ฐ์ดํฐ ๋ถ์ ํ ๋งคํ
const pageInfos = new Map(
pages.map((page, index) => {
const parsed = parsePage(page, index, word);
return [parsed.url, parsed];
})
);
//์ธ๋ถ ๋งํฌ ์ ์ ๋ถ์ฌ ์ฒ๋ฆฌ
for (const page of pageInfos.values()) {
const additionalPoint = page.basePoint / page.externalUrls.length;
for (const externalUrl of page.externalUrls) {
if (pageInfos.has(externalUrl)) {
pageInfos.get(externalUrl).totalPoint += additionalPoint;
}
}
}
//์ด์ ์๋ฅผ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๊ณ ์ฒซ๋ฒ์งธ ํ์ด์ง์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํ
return [...pageInfos.values()].sort((a, b) => b.totalPoint - a.totalPoint)[0].index;
}
ํ์ง๋ง ์ด๋ ๊ฒ ์์ฑํ ์ฝ๋์ ๊ฒฝ์ฐ ์
๋ ฅ ์์ 2๊ฐ์ง๋ ํต๊ณผํ์ง๋ง, ํ
์คํธ ์ผ์ด์ค 1,2,9,12๋ฅผ ํต๊ณผํ์ง ๋ชปํด ์คํจํ์ต๋๋ค. ํ
์คํธ์ผ์ด์ค๋ ๋น๊ณต๊ฐ์ด๊ธฐ ๋๋ฌธ์ ์ด๋ค ์
๋ ฅ์ ์คํจํ๋์ง ์ง์ ์ฐพ์ ํด๊ฒฐํด์ผ๋ง ํ์ต๋๋ค. ๋๋ฌธ์ ๋ฌธ์ ์ ์ ํ ๋ชจ๋ ์กฐ๊ฑด์ ๋ฐ๋ก๊ฐ ๋ ๋งํ ํ
์คํธ ์ผ์ด์ค๋ฅผ ์ง์ ์ถ๊ฐํด์ ์คํํด๋ดค์ต๋๋ค. ๊ทธ๋ฌ๋ ์ค ๋ฌธ์ ์กฐ๊ฑด์ ์๋ฅผ๋ค์ด ๊ฒ์์ด๊ฐ "aba" ์ผ ๋, "abab abababa"๋ ๋จ์ด ๋จ์๋ก ์ผ์นํ๋๊ฒ ์์ผ๋, ๊ธฐ๋ณธ ์ ์๋ 0์ ์ด ๋๋ค
๋ผ๋ ๋ถ๋ถ์ ๊ตฌํํ ํ
์คํธ ์ผ์ด์ค๋ฅผ ์ถ๊ฐํ ์๊ฐ ๋๋์ด ์ ์ฝ๋๊ฐ ํต๊ณผํ์ง ๋ชปํ๋ ํ
์คํธ์ผ์ด์ค๋ฅผ ์ฐพ๊ฒ ๋์์ต๋๋ค.
์์ธ์ ๋ฐ๋ก ๊ธฐ๋ณธ ์ ์๋ฅผ ๊ณ์ฐํ๋ ๋ถ๋ถ์์ ์ ์ง๋ฅธ ์ค์ ๋๋ฌธ์ด์์ต๋๋ค.
const basePoint = (body.match(new RegExp(`[^a-z]${word}[^a-z]`, 'ig')) || []).length;
๋จ์ํ bodyํ๊ทธ ๋ด์๋ ์ต์ํ ๋จ์ด ์์ชฝ์ ๋์ด์ฐ๊ธฐ๋ผ๋ ์์ํ ๋ ์๋ค๊ฐ ์๋ฌธ์๊ฐ ์๋๋ฐ, word์ ์ผ์นํ๋ฉด ๋ ๊ฑฐ๋ผ๊ณ ์๊ฐํ์๋๋ฐ, ์ด๋ ๊ฒ ์ฒ๋ฆฌํ๋ ๊ฒฝ์ฐ ์๋ฌธ์ ์ฌ์ด์ ํ๊ธ์๋ง ์กด์ฌํ๋ ๊ฒฝ์ฐ ๋ค์ ์๋ฌธ์๋ฅผ ๊ทธ๋ฅ ๋์ด๊ฐ๋ ๊ฒฝ์ฐ๊ฐ ๋ฐ์ํ์ต๋๋ค.
์๋ฅผ ๋ค์ด 1abc2def3ghi4
๋ผ๋ ๋ฌธ์์ด์ ๋ํด ์ ๊ท์ /[^a-z][a-z]+[^a-z]/g
๋ฅผ ์ฒ๋ฆฌํ๋ฉด ๊ฒฐ๊ณผ๋ ['1abc2', '3ghi4']
์
๋๋ค. 2def3
์ด ํฌํจ๋์ง ์์ ์ด์ ๋ ์ ๊ท์์ด ์๋์ ๊ฐ์ด ์๋ํ๊ธฐ ๋๋ฌธ์
๋๋ค.
์ฒ๋ฆฌ์ค์ธ ๋ฌธ์ | โฆ | ํ์ธ์ค์ธ ๋ฌธ์์ด | ๋จ์ ๋ฌธ์์ด |
---|---|---|---|
1 | [^a-z] ์ผ์น | 1 | abc2def3ghi4 |
a | [a-z]+ ์ผ์น | 1a | bc2def3ghi4 |
b | [a-z]+ ์ผ์น | 1ab | c2def3ghi4 |
c | [a-z]+ ์ผ์น | 1abc | 2def3ghi4 |
2 | [^a-z] ์ผ์น - ๋งค์นญ ์๋ฃ | 1abc2 | def3ghi4 |
d | [^a-z] ๋ถ์ผ์น | ef3ghi4 | |
e | [^a-z] ๋ถ์ผ์น | f3ghi4 | |
f | [^a-z] ๋ถ์ผ์น | 3ghi4 | |
3 | [^a-z] ์ผ์น | 3 | ghi4 |
g | [a-z]+ ์ผ์น | 3g | hi4 |
h | [a-z]+ ์ผ์น | 3gh | i4 |
i | [a-z]+ ์ผ์น | 3ghi | 4 |
4 | [^a-z] ์ผ์น - ๋งค์นญ ์๋ฃ | 3ghi4 |
๊ณ ๋ ์ธ์์ ์์ฐ def๋ ๋ฑ์ด ํฐ์ ธ๋ฒ๋ ธ์ต๋๋ค!
์ด๋ฐ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๋ชจ๋ ์๋จ์ด๋ฅผ ๊ฐ์ ธ์จ ๋ค ์ฐพ์์ผํ ๋จ์ด์ ๋น๊ตํด ์ผ์นํ๋ ํ์๋ฅผ ๊ธฐ๋ณธ์ ์๋ก ์ฌ์ฉํ๋๋ก ์๋์ ๊ฐ์ด ์์ ํ๊ณ , ์ด์ ๋ชจ๋ ํ ์คํธ ์ผ์ด์ค๋ฅผ ์ ํต๊ณผํฉ๋๋ค.
const basePoint = body.match(/[a-z]+/gi).filter((match) => match === word.toLowerCase()).length;
๋ค๋ฅธ ๋ถ๋ค์ ์ ์ฒ๋ผ ์ ๊ท์์ ์ ๋ชจ๋ฅด๋ฉด์ ์ ๊ท์์ ๋งน์ ํ๋ ์ผ์ด ์๊ธธ ๋ฐ๋๋๋คโฆ ์ด๊ฑธ ๋ชฐ๋ผ์ ์ฝ๋์ ์จ๊ฐ ๋ถ๋ถ์ ๋ค ๊ฐ์ ์๊ณ ๋๋ฆฌ ์ณค์ด์ใ ใ
const parsePage = (page, index, word) => {
//ํ์ด์ง์ ์ฃผ์์ ์ธ๋ถ ๋งํฌ๋ ์ ๊ท์์ ํตํด ์ฐพ์ ์ ์๋ค
const url = page.match(/<meta property="og:url" content="(.+?)"/i)[1];
//ํ์ด์ง์ body ๋ด์ฉ์ ๊ฐ์ ธ์ ๋ฏธ๋ฆฌ ์๋ฌธ์ํํ๋ค
let body = page.match(/<body>([wW]+)</body>/i)[1].toLowerCase();
//replaceAll์ ์ฝ๋ฐฑ ํจ์๋ก ์ธ๋ถ ๋งํฌ๋ฅผ ์์งํ๊ณ body์์ ์ ๊ฑฐ
const externalUrls = [];
body = body.replaceAll(/<a href="(.+?)">/gi, (_, externalUrl) => {
externalUrls.push(externalUrl);
return '';
});
//body์ ๋ชจ๋ ์๋จ์ด๋ฅผ ๊ฒ์ฌํด ํ์ด์ง์ ๊ธฐ๋ณธ ์ ์๋ฅผ ๊ณ์ฐ
const basePoint = body.match(/[a-z]+/gi).filter((match) => match === word.toLowerCase()).length;
return {
index,
url,
externalUrls,
basePoint,
totalPoint: basePoint
};
};
function solution(word, pages) {
//์
๋ ฅ๋ ๋ฐ์ดํฐ ๋ถ์ ํ ๋งคํ
const pageInfos = new Map(
pages.map((page, index) => {
const parsed = parsePage(page, index, word);
return [parsed.url, parsed];
})
);
//๋งคํ๋ ๋ฐ์ดํฐ๋ก ์ธ๋ถ ๋งํฌ ์ ์ ๋ถ์ฌ ์ฒ๋ฆฌ
for (const page of pageInfos.values()) {
const additionalPoint = page.basePoint / page.externalUrls.length;
for (const externalUrl of page.externalUrls) {
if (pageInfos.has(externalUrl)) {
pageInfos.get(externalUrl).totalPoint += additionalPoint;
}
}
}
//์ด์ ์๋ฅผ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๊ณ ์ฒซ๋ฒ์งธ ํ์ด์ง์ ์ธ๋ฑ์ค๋ฅผ ๋ฐํ
return [...pageInfos.values()].sort((a, b) => b.totalPoint - a.totalPoint)[0].index;
}