문자열 myString
과 pat
가 주어집니다. myString
의 부분 문자열중 pat
로 끝나는 가장 긴 부분 문자열을 찾아서 return 하는 solution 함수를 완성해 주세요.
function solution(myString, pat) {
const index = myString.lastIndexOf(pat);
return myString.slice(0, index + pat.length);
}
const solution = (myString, pat) => myString.slice(0, myString.lastIndexOf(pat)) + pat;
const str1 = "AbCdEFG";
const str2 = "AAAAaaaa";
const pat1 = "dE";
const pat2 = "a";
const index1 = str1.lastIndexOf(pat1); // 3
str1.slice(0, index1); // AbC
const index2 = str2.lastIndexOf(pat2); // 7
str2.slice(0, index2); // AAAAaaa
str1.slice(0, index1 + pat1.length); // AbCdE
str2.slice(0, index2 + pat2.length); // AAAAaaaa
str1.slice(0, index1) + pat1; // AbCdE
str2.slice(0, index2) + pat2; // AAAAaaaa