문자열 내 p와 y의 개수

ccy·2022년 1월 18일
0

알고리즘(JS)

목록 보기
16/31

문제 설명
대문자와 소문자가 섞여있는 문자열 s가 주어집니다. s에 'p'의 개수와 'y'의 개수를 비교해 같으면 True, 다르면 False를 return 하는 solution를 완성하세요. 'p', 'y' 모두 하나도 없는 경우는 항상 True를 리턴합니다. 단, 개수를 비교할 때 대문자와 소문자는 구별하지 않습니다.

예를 들어 s가 "pPoooyY"면 true를 return하고 "Pyy"라면 false를 return합니다.

제한사항
문자열 s의 길이 : 50 이하의 자연수
문자열 s는 알파벳으로만 이루어져 있습니다.


function solution(s){
    let pCount =0;
    let yCount = 0;

  for(let i =0; i<s.length;i++){
    
    if(s[i] === "p" || s[i] === "P"){
      pCount++
    }else if(s[i] === 'y' || s[i] === "Y"){
      yCount++
    }
  }
  if(pCount === yCount){
    return true
  }else{
    return false
  }
}

solution("Pyy")
function solution(s){
  return s.toUpperCase().split("P").length===s.toUpperCase().split("Y").length


}

solution("Pyy")
solution("pPoooyY")

Syntax

Parameters
separator Optional
The pattern describing where each split should occur. The separator can be a simple string or it can be a regular expression.

The simplest case is when separator is just a single character; this is used to split a delimited string. For example, a string containing tab separated values (TSV) could be parsed by passing a tab character as the separator, like this: myString.split("\t").
If separator contains multiple characters, that entire character sequence must be found in order to split.
If separator is omitted or does not occur in str, the returned array contains one element consisting of the entire string.
If separator appears at the beginning (or end) of the string, it still has the effect of splitting. The result is an empty (i.e. zero length) string, which appears at the first (or last) position of the returned array.
If separator is an empty string (""), str is converted to an array of each of its UTF-16 "characters".
Warning: When the empty string ("") is used as a separator, the string is not split by user-perceived characters (grapheme clusters) or unicode characters (codepoints), but by UTF-16 codeunits. This destroys surrogate pairs. See “How do you get a string to a character array in JavaScript?” on StackOverflow.

limit Optional
A non-negative integer specifying a limit on the number of substrings to be included in the array. If provided, splits the string at each occurrence of the specified separator, but stops when limit entries have been placed in the array. Any leftover text is not included in the array at all.

The array may contain fewer entries than limit if the end of the string is reached before the limit is reached.
If limit is 0, [] is returned.

Return value
An Array of strings, split at each point where the separator occurs in the given string.

Description
When found, separator is removed from the string, and the substrings are returned in an array.

If separator is a regular expression with capturing parentheses, then each time separator matches, the results (including any undefined results) of the capturing parentheses are spliced into the output array.

If the separator is an array, then that Array is coerced to a String and used as a separator.

profile
개발배우는중

0개의 댓글

관련 채용 정보