Sliding Window
function solution(str1, str2) { let i = 0; let j = 0; while (j < str2.length) { if (str2[j] === str1[i]) { i += 1; } //이때 i = 3 if (i === str1.length) { return true; } j++; // j = 0 [a] , i = 0[6] // j = 1 [b] , i = 0[6] // j = 2 [6] , i = 0[6] // j = 3 [C] , i = 1[C] // j = 4 [D] , i = 2[D] // j = 5 [E] , i = 3[] // j = 6 [4] , i = 3[] // j = 7 [4] , i = 3[] // j = 8 [3] , i = 3[] // j = 9 [f] ..
2023. 3. 17.
TIL 20230316(온보딩 9일차)
function solution(array, n) { let count = 0 for(i = 0; i < array.length; i++){ if(array[i] === n){ count += 1 } } return count } console.log(solution([1,1,2,3,4,5], 1)) console.log(solution([0,2,3,4], 1)) array 에 n 이 몇개 있는지 구하는 코드이다. 변수 count를 0으로 할당해놓고 반복문을 사용해 array.length 미만으로 반복을 돌게한다. 이때 반복문 안에 조건문을 쓰는데 만약 array[i]번째가 n 과 같다면 count 에 1을 더하게 하여 array 내에 n이 있을때마다 카운트가 올라가게 했다. function soluti..
2023. 3. 17.