[leetcode, JS] 2315. Count Asterisks

mxxn·2023년 12월 28일
0

leetcode

목록 보기
156/198

문제

문제 링크 : Count Asterisks

풀이

/**
 * @param {string} s
 * @return {number}
 */
var countAsterisks = function(s) {
    let pair=0, count=0;
    for(let c of s){
        if(c == '|')    pair++;
        if(pair % 2 == 0 && c == '*')    count++;
    }
    return count;
};
  1. '|' pair 안에 들어가 있지 않은 Asterisk의 개수를 찾으면 되기 때문에
  2. 해당 조건에 맞을 때만 count ++
  • Runtime 79 ms, Memory 43.06 MB

다른 풀이

/**
 * @param {string} s
 * @return {number}
 */
var countAsterisks = function(s) {
    return s.split('|').filter((_, i) => i % 2 === 0).join('').split('*').length - 1;
};
  1. filter와 split을 사용한 풀이
  • Runtime 48 ms, Memory 42.63 MB
profile
내일도 글쓰기

0개의 댓글