728x90
<ํ๋ฆฐ ๋ต>
function solution(A, B) {
const aArr = A.split("");
const bArr = B.split("");
// console.log(aArr, bArr) //[ 'h', 'e', 'l', 'l', 'o' ] [ 'o', 'h', 'e', 'l', 'l' ]
let matchIndex = [];
for (let i = 0; i < aArr.length; i++) {
aArr[0] === bArr[i] && matchIndex.push(i);
}
console.log(matchIndex);
let result = 0;
matchIndex.sort((a, b) => b - a).map(index => {
let cArr = new Array(aArr.length);
aArr.map((v, i) => i + index >= A.length ? cArr[i + index - aArr.length] = v : cArr[i + index] = v)
console.log("cArr", cArr);
if (cArr.join("") === bArr.join("")) {
result = index;
return;
} else {
result = -1;
}
})
console.log("result", result);
return result;
}
solution("hello", "ohell");
solution("apple", "elppa");
solution("atat", "tata");
solution("abc", "abc");
solution("abcabc", "bbbaaa")
๋ฐฐ์ด์์ ๋ง๋ index ๊ตฌํด์, ๊ทธ index ๋ฐฐ์ด์์ ํฐ ์๋ถํฐ ๋น๊ต -> ์์ ์๊ฐ ๋์ค๋ฉด result ๋ฅผ ๋ฎ์ด์์ฐ๋๋ก ๋ง๋ค์๋ค.
ํ ์คํฌ ์ฝ๋ ํต๊ณผ๋ ์ ํ๋๋ฐ ๋ง์ง๋ง์ ํต๊ณผ ๋ชปํด์ ๋ญ๊น ๊ณ ๋ฏผํ์.
๋ฐฐ์ด๋ก ๋ฐ๊ฟ์ ์ ์๋์ง ๊ณ ๋ฏผํ๋ค๊ฐ ๋ค๋ฅธ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํด๋ณด๊ธฐ๋ก ํ๋ค.
<์ ๋ต>
function solution(A, B) {
let arr = [...A]
for(let i = 0; i< arr.length; i++){
if(A === B)
return i;
else{
arr.unshift(arr.pop())
if(arr.join('') === B)
return i+1;
}
}
return -1;
}
์ค..
๊ทธ๋ฅ string ์ผ๋ก๋ ํด๊ฒฐํ ์ ์๊ตฌ๋ ๊ฐํํ๋๋ฐ
let solution=(a,b)=>(b+b).indexOf(a)
.....?????
์๋???
์ด๋ ๊ฒ ๊ฐ๋จํ ํด๊ฒฐํ ์ ์์๋ค๋,,,
์ญ์ ๋ณต์กํ ๋ฌธ์ ์ผ ์๋ก ๊ฐ๋จํ ๋ณผ ์ ์๋๊ฒ ์ต๊ณ ์ธ ๋ฏ ํ๋ค,,,
728x90
๋ฐ์ํ