in repeated string finding the total number of time the occurrence of any character

count occurrences of a character in a repeated string
java program to count number of repeated characters in a string
how to count the number of occurrences of a character in a string in java
how to count repeated characters in a string php
c program to count the number of occurrences of a character in a string
count repeated characters in a string python
count occurrences of character in string java 8
how to count the number of repeated characters in a string in c++

Given an integer,n, find and print the number of letter a's in the first n letters of 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.

static long repeatedString(String s, long n) {
        long len = s.length(), count = 0;
        StringBuilder sb = new StringBuilder("");
        char[] c = s.toCharArray();
        for (int i = 0; i < n; i++) {
            sb.append(c[(i % len)]);
            if (sb.charAt(i) == 'a')
                count++;
        }
        return count;
    }

it is showing error

incompatible types: possible lossy conversion from long to int sb.append(c[i%len]);

if i am type casting the len then it is not passing the test case for the value whose length is greater than 10^9 for example if my input is

a 1000000000000

then the output must be 1000000000000

note-> for any given input i have to calculate the total number of 'a' present in that given string

EDIT:i am calling my function as

long result = repeatedString("a", 1000000000000);

You don't need a StringBuilder. Get the result you want by iterating the initial string and multiply it as many times as needed, meaning by (n / len), then add what is left by the division:

static long repeatedString(String s, long n) {
    long len = s.length();

    if (len == 0)
        return 0;

    long rep = n / len;
    long count = 0;

    for (int i = 0; i < len; i++) {
        if (s.charAt(i) == 'a')
            count++;
    }

    count *= rep;

    long rest = n % len;

    for (int i = 0; i < rest; i++) {
        if (s.charAt(i) == 'a')
            count++;
    }
    return count;
}

in repeated string finding the total number of time the occurrence of , static long repeatedString(String s, long n) { long len = s.length(); if (len want to find number of occurrences of a particular character in a string. clear, for that all you need to do is to add the characters of your string to the  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.

Part of your question seemed a bit vague, but from the last line, I get that, you want to find number of occurrences of a particular character in a string.

You can use a HashMap to add the unique characters of your String and set the value as the number of occurrences of the character.

What I am saying, looks something like this in code:

HashMap<Character,Integer> hm = new HashMap<Character,Integer>();

char[] str = s.toCharArray();

for(char c : str) {

    if(hm.containsKey(c))
        hm.put(c, hm.get(c)+1);

    else
        hm.put(c, 1);
}

Now you can just choose the character of your choice in the string to get it's number of occurrences, like this:

if(hm.get(character)!=null)
    ans = hm.get(character);

else
    ans = 0;

There is also a library, I am making for this here.

Edit:

From the edit, the question is much more clear, for that all you need to do is to add the characters of your string to the previous string, until the length is met.

After that, you can use the new string like in the code, I've given here.

Java program to find the occurrence of a character in a string, In this program we are finding the occurrence of each character in a String. To do this we are first creating an array of size 256 (ASCII upper range), the idea here is to new int[256]; //String length int len = str.length(); /* This array holds the occurrence of each char, For Java program to find duplicate characters in a String Formula to Count the Number of Occurrences of a Text String in a Range. =SUM (LEN ( range )-LEN (SUBSTITUTE ( range ,"text","")))/LEN ("text") Where range is the cell range in question and "text" is replaced by the specific text string that you want to count. Note.

I think you do not have to use StringBuidler (moreover, for big n, this could flow OutOfMemoryError), but count required letters on the fly.

public static long repeatedString(String str, long n, char ch) {
    long count = 0;

    for (int i = 0; i < str.length(); i++) {
        if (str.charAt(i) == ch)
            continue;

        count += n / str.length();
        count += i < n % str.length() ? 1 : 0;
    }

    return count;
}

Count occurrences of character in String java program code with , Read Also : Find first Non-Repeated Character in the String 1. Pseudo code/​Logic We store the input string in an character array (below z) .(We want to count total number of times each alphabet appears in the string java program. Code :. Python | Count occurrences of a character in string Given a string, the task is to count the frequency of a single character in that string. This particular operation on string is quite useful in many applications such as removing duplicates or detecting unwanted characters.

substr_count - Manual, substr_count() returns the number of times the needle substring occurs in the haystack string. characters used in a string; strpos() - Find the position of the first occurrence of a substring a simple version for an array needle (multiply sub-strings): str_​pad · str_​repeat · str_​replace · str_​rot13 · str_​shuffle · str_​split  Write a Java Program to Find the Count of Occurrences of Each Character in a String. Well, if you are appearing for any IT company interview this is the most expected question in your Technical

Count occurrences of pattern in strings - MATLAB count, If pattern is an array containing multiple patterns, then count returns the sum of the occurrences of all Count the number of occurrences of the sequence of characters, red , in string arrays. Count the number of times al occurs in the word alphabetical . Calculate with arrays that have more rows than fit in memory. As soon as we find a character that occurs more than once, we return the character. This step can be done in O (N Log N) time. An efficient solution is to use Hashing to solve this in O (N) time on average. Create an empty hash. Scan each character of input string and insert values to each keys in the hash.

Python Program to Count Occurrence of a Character in a String, Python Program to Count Occurrence of a Character in a String : Write a Python Program if(string[i] == char): count = count + 1 print("The total Number of Times ", char, " has Occurred = " , count) to check whether any character in a string is equal to the given character or not. C Program to Calculate Compound Interest. Each time you find different character than previous one, it means the run (consecutive repeating alphabet) is ended, and so you should note down the length of current run (i.e., the value of count) and then reset the count.

Comments
  • sb.append(c[(int) (i%len)]);
  • already tried and o/p is error only
  • Correct solution, but you do not have to iterate over the string TWICE.
  • Right, the 2nd time for the substring.
  • Yes, I see. This is unnecessary. You can move it to the first loop. What if n = str.length() - 1 and n = 1_000_000.
  • @RAVIKUMARSHARMACSE16 you don't need it. You only use StringBuilder when you want to construct large strings by repeated appending. You don;t need this to solve this problem.
  • @RAVIKUMARSHARMACSE16 you chose a method that is inefficient. The solution is simpler.
  • char[] str = s.toCharArray(); creates extra char array in the memory. According this case, s.charAt(i) is much better to iterate over characters.