Repeated String

Repeated String

Lilah has a string, s, of lowercase English letters that she repeated infinitely many times.

Given an integer, n, find and print the number of letter a’s in the first letters of Lilah’s infinite string.

For example, if the string s = ‘abcac’ and n = 10, the substring we consider is abcacabcac, the first 10 characters of her infinite string. There are 4 occurrences of a in the substring.

Function Description

Complete the repeatedString function in the editor below. It should return an integer representing the number of occurrences of a in the prefix of length n in the infinitely repeating string.

repeatedString has the following parameter(s):

  • s: a string to repeat
  • n: the number of characters to consider

Input Format

The first line contains a single string, s.

The second line contains an integer, n.

Constraints

  • 1 <= |s| <= 100
  • 1 <= n <= 1012
  • For 25% of the test cases, n <= 106.

Output Format

Print a single integer denoting the number of letter a’s in the first n letters of the infinite string created by repeating s infinitely many times.

Sample Input 0

1
2
aba
10

Sample Output 0

1
7

Explanation 0

The first n = 10 letters of the infinite string are abaabaabaa. Because there are 7 a’s, we print 7 on a new line.

Sample Input 1

1
2
a
1000000000000

Sample Output 1

1
1000000000000

Explanation 1

Because all of the first n = 1000000000000 letters of the infinite string are a, we print 1000000000000 on a new line.


Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Complete the cutTheSticks function below.
function cutTheSticks(arr) {
let result = [];

while (arr.length > 0) {
result.push(arr.length);

arr.sort((a, b) => (a - b));

let front = arr[0];

arr.reduce((target, value, index) => {
arr[index] -= front;

return target;
}, []);

let remove = arr.lastIndexOf(0) + 1;

arr.splice(0, remove);
}

return result;
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×