Computer Organization. Discrete Mathematics. Ethical Hacking. Computer Graphics. Software Engineering. Web Technology. Cyber Security. C Programming. Control System. Data Mining. Data Warehouse. Javatpoint Services JavaTpoint offers too many high quality services.
Picture perfect To accomplish this task, we will maintain an array called freq with same size of the length of the string. For example : Frequency of p in above string is 2.
Algorithm Define a string. Define an array freq with the same size of the string. To understand this example, you should have the knowledge of the following Java programming topics:. In the above program, the length of the given string, str , is found using the string method length. We loop through each character in the string using charAt function which takes the index i and returns the character in the given index. We compare each character to the given character ch.
If it's a match, we increase the value of frequency by 1. In the end, we get the total occurrence of a character stored in frequency and print it. Course Index Explore Programiz. Java for Loop. Arrays in Java. Interfaces in Java. Java ArrayList. Popular Examples Check prime number. Print the Fibonacci series. We will create an integer array of size 26 and will store the count of each character in this array. The indexes of the count array will represent the characters.
For example, index 0 will represent a, 1 as b, and so on. To find the indexes from characters will follow the below steps:. We can use HashMap to store the character as key and their count as value.
Using a map we can store all the other characters A-Z as well. Translate Language. December 28, Example HashMap ; import java. Java Program. By Akshay Kumar. Post a Comment. Number of Substrings with the count of each Character as K A String s comprised of digits from 0 to 9 contains a perfect substring if all the elements within a substring occur exactly k times.
0コメント