Given an integer N and a lowercase string. The string is repeated infinitely. The task is to find the No. of occurrences of a given character x in first N letters.
Input : N = 10 str = "abcac" Output : 4 Explanation: "abcacabcac" is the substring from the infinitely repeated string. In first 10 letters 'a' occurs 4 times. Input: N = 10, str = "aba" Output : 7
1. Find the occurrences of character ‘a’ in the given string.
2. Find the No. of repetitions which are required to find the ‘a’ occurrences.
3. Multiply the single string occurrences to the No. of repetitions.
4. If given n is not the multiple of given string size then we will find the ‘a’ occurrences in the remaining substring.
Below is the implementation of above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Map every character of one string to another such that all occurrences are mapped to the same character
- Find the first repeated character in a string
- Remove all occurrences of a character in a string
- Find repeated character present first in a string
- Print the string by ignoring alternate occurrences of any character
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Efficiently find first repeated character in a string without using any additional data structure in one traversal
- Count occurrences of a string that can be constructed from another given string
- Count occurrences of a word in string
- Count of occurrences of a "1(0+)1" pattern in a string
- Program to count occurrence of a given character in a string
- Count of strings that can be formed from another string using each character at-most once
- Count of number of given string in 2D character array
- Index of character depending on frequency count in string
- Count occurrence of a given character in a string using Stream API in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.