JS Repeated string Hackerrank Challenge
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Problem Overview
The Hackerrank challenge "Repeated String" is a straightforward yet intriguing problem that evaluates a programmer's ability in string manipulation and basic counting techniques. The challenge is simple: you are given a string `s` and an integer `n`. You need to determine how many times the letter 'a' appears in the first `n` characters of a string created by infinitely repeating `s`.
Problem Statement
- You have a string `s`.
- You need to construct an infinite sequence by repeating this string consecutively.
- Determine and return the number of occurrences of the letter 'a' in the first `n` characters of this infinite sequence.
Examples
Let's consider a few examples for clarity:
- Example 1:
- `s = "abcac"`, `n = 10`
- The infinite sequence would start as "abcacabcacabcac..."
- The first 10 characters are "abcacabcac", which contain 4 occurrences of 'a'.
- Output: `4`
- Example 2:
- `s = "aba"`, `n = 10`
- The infinite sequence would start as "abaabaabaa..."
- The first 10 characters are "abaabaabaa", which contain 7 occurrences of 'a'.
- Output: `7`
Technical Explanation
To solve this problem efficiently, observe that the infinite repetition nature of the input string lends itself well to modular arithmetic and integer division. Here’s how you can break it down:
- Count Occurrences in Original String:First, count how many times 'a' appears in string `s`. Let’s denote this count as `countA`.
- Full Repeats of String:Determine how many full times the string `s` can fit within the given length `n`. This is calculated with integer division: `full_repeats = n // len(s)`.
- Partial String Analysis:Calculate how many characters are in the leftover or partial repeat. This is found using the modulus operation: `remaining_chars = n % len(s)`.
- Calculate Total 'a's:Multiply the count of 'a's in a single instance of `s` by the number of full times `s` repeats. Then count the 'a's in the substring that accounts for the leftover positions.
- Total `'a's` in full repeats: `countA * full_repeats`
- Total `'a's` in the remaining part: `count('a' in s[:remaining_chars])`
- Sum it Up:Add the results from the full repeats and the leftover part to get the total number of 'a's in the first `n` characters.
Pseudocode
Related reading
- Justify string algorithm
- K- Means algorithm
- K-means algorithm variation with equal cluster size
- K-Means Lloyd,Forgy,MacQueen,Hartigan-Wong
- JS what's the promises equivalent of async.each?
- JSLint is suddenly reporting Use the function form of use strict
- k-vertex connectivity of a graph
- K mutually exclusive routes in a graph

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.