반복 문자열 코드 문제 해결
1541 단어 codechallengehackerrank
"n"의 정수를 지정하여 Lilah 무한 문자열의 첫 번째 문자에서 "a"의 숫자를 찾아 인쇄합니다.
예를 들어 만약 문자열 s='abcac'와 n=10이 있다면 우리가 고려한 하위 문자열은 abcacacac이고 이것은 무한 문자열의 10개 문자이다.하위 문자열에 "a"가 네 번 나타납니다.
기능 설명
다음 편집기에서 repeatedString 함수를 완성합니다.무한 반복 문자열의 길이 접두사에 'a' 가 나타나는 횟수를 나타내는 정수를 되돌려야 합니다.
repeatedString에는 다음과 같은 매개변수가 있습니다.
4
4
입력 형식
첫 번째 줄은 문자열 s를 포함하고, 두 번째 줄은 정수 n을 포함합니다.
출력 형식
무한히 여러 번 반복해서 만든 무한 문자열의 첫 번째 알파벳 a의 수량을 표시하는 정수를 인쇄합니다.
샘플 입력
아바
10
샘플 출력
7
해석하다
무한 문자열의 첫 번째 자모는 "abaa"입니다.a가 있기 때문에, 우리는 새 줄에 인쇄합니다.
솔루션
function repeatedString($s, $n) {
// check the number of occurrences of letter 'a' on given string $s
$a_occurrences_in_s = substr_count($s, 'a');
// check how many occurrences of string $s belongs to an repeated string $s of length of $n
$s_occurrences_qty = floor($n / strlen($s));
// get the $s occurrences left
$s_occurrences_left = $n - ($s_occurrences_qty * strlen($s));
// left string
$s_left = substr($s, 0, $s_occurrences_left);
// 'a' occurrences in left string
$a_occurrences_in_s_left = substr_count($s_left, 'a');
// occurrencies of 'a' on the entire repeated string of $s with a length of $n
return ($a_occurrences_in_s * $s_occurrences_qty) + $a_occurrences_in_s_left;
}
Reference
이 문제에 관하여(반복 문자열 코드 문제 해결), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/rezende79/repeated-string-code-challenge-solved-278a텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)