JavaScript
Repeated String
Hackerrank Challenge
Coding Interview
Algorithm

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.

Practice algorithms

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

  1. You have a string `s`.
  2. You need to construct an infinite sequence by repeating this string consecutively.
  3. 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:

  1. Count Occurrences in Original String:
    First, count how many times 'a' appears in string `s`. Let’s denote this count as `countA`.
  2. 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)`.
  3. 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)`.
  4. 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])`
  5. 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.