C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to find maximum and minimum occurring character in a stringExplanationIn this program, we need to count each character present in the string and find out the maximum and minimum occurring character. Grass is greener on the other side In above example, character 'a' is occurred only once in the string. So, it is minimum occurring character and is highlighted by red. Character e has occurred maximum number of times in the entire string i.e. 6 times. Hence, it is the maximum occurring character and is highlighted by green. Algorithm
SolutionPythonstring = "grass is greener on the other side"; freq = [None] * len(string); minChar = string[0]; maxChar = string[0]; #Count each word in given string and store in array freq for i in range(0, len(string)): freq[i] = 1; for j in range(i+1, len(string)): if(string[i] == string[j] and string[i] != ' ' and string[i] != '0'): freq[i] = freq[i] + 1; #Set string[j] to 0 to avoid printing visited character string = string[ : j] + '0' + string[j+1: ]; #Determine minimum and maximum occurring characters min = max = freq[0]; for i in range(0, len(freq)): #If min is greater than frequency of a character #then, store frequency in min and corresponding character in minChar if(min > freq[i] and freq[i] != '0'): min = freq[i]; minChar = string[i]; #If max is less than frequency of a character #then, store frequency in max and corresponding character in maxChar if(max < freq[i]): max = freq[i]; maxChar = string[i]; print("Minimum occurring character: " + minChar); print("Maximum occurring character: " + maxChar); Output: Minimum occurring character: a Maximum occurring character: e C#include <stdio.h> #include <string.h> int main() { char string[] = "grass is greener on the other side"; int i, j, min, max, length = strlen(string); char minChar = string[0], maxChar = string[0]; int freq[length]; //Count each word in given string and store in array freq for(i = 0; i < length; i++) { freq[i] = 1; for(j = i+1; j < length; j++) { if(string[i] == string[j] && string[i] != ' ' && string[i] != '0') { freq[i]++; //Set string[j] to 0 to avoid printing visited character string[j] = '0'; } } } //Determine minimum and maximum occurring characters min = max = freq[0]; for(i = 0; i < length; i++) { //If min is greater than frequency of a character //then, store frequency in min and corresponding character in minChar if(min > freq[i] && freq[i] != '0') { min = freq[i]; minChar = string[i]; } //If max is less than frequency of a character //then, store frequency in max and corresponding character in maxChar if(max < freq[i]) { max = freq[i]; maxChar = string[i]; } } printf("Minimum occurring character: %c\n", minChar); printf("Maximum occurring character: %c", maxChar); return 0; } Output: Minimum occurring character: a Maximum occurring character: e JAVApublic class Characters { public static void main(String[] args) { String str = "grass is greener on the other side"; int[] freq = new int[str.length()]; char minChar = str.charAt(0), maxChar = str.charAt(0); int i, j, min, max; //Converts given string into character array char string[] = str.toCharArray(); //Count each word in given string and store in array freq for(i = 0; i < string.length; i++) { freq[i] = 1; for(j = i+1; j < string.length; j++) { if(string[i] == string[j] && string[i] != ' ' && string[i] != '0') { freq[i]++; //Set string[j] to 0 to avoid printing visited character string[j] = '0'; } } } //Determine minimum and maximum occurring characters min = max = freq[0]; for(i = 0; i <freq.length; i++) { //If min is greater than frequency of a character //then, store frequency in min and corresponding character in minChar if(min > freq[i] && freq[i] != '0') { min = freq[i]; minChar = string[i]; } //If max is less than frequency of a character //then, store frequency in max and corresponding character in maxChar if(max < freq[i]) { max = freq[i]; maxChar = string[i]; } } System.out.println("Minimum occurring character: " + minChar); System.out.println("Maximum occurring character: " + maxChar); } } Output: Minimum occurring character: a Maximum occurring character: e C#using System; public class Characters { public static void Main() { String str = "grass is greener on the other side"; int[] freq = new int[str.Length]; char minChar = str[0], maxChar = str[0]; int i, j, min, max; //Converts given string into character array char[] string1 = str.ToCharArray(); //Count each word in given string and store in array freq for(i = 0; i < string1.Length; i++) { freq[i] = 1; for(j = i+1; j < string1.Length; j++) { if(string1[i] == string1[j] && string1[i] != ' ' && string1[i] != '0') { freq[i]++; //Set string1[j] to 0 to avoid printing visited character string1[j] = '0'; } } } //Determine minimum and maximum occurring characters min = max = freq[0]; for(i = 0; i < freq.Length; i++) { //If min is greater than frequency of a character //then, store frequency in min and corresponding character in minChar if(min > freq[i] && freq[i] != '0') { min = freq[i]; minChar = string1[i]; } //If max is less than frequency of a character //then, store frequency in max and corresponding character in maxChar if(max < freq[i]) { max = freq[i]; maxChar = string1[i]; } } Console.WriteLine("Minimum occurring character: " + minChar); Console.WriteLine("Maximum occurring character: " + maxChar); } } Output: Minimum occurring character: a Maximum occurring character: e PHP<!DOCTYPE html> <html> <body> <?php $string = "grass is greener on the other side"; $freq = array(); $minChar = $maxChar = $string[0]; //Count each word in given string and store in array freq for($i = 0; $i < strlen($string); $i++) { array_push($freq, 1); for($j = $i+1; $j < strlen($string); $j++) { if($string[$i] == $string[$j] && $string[$i] != ' ' && $string[$i] != '0') { $freq[$i]++; //Set $string[$j] to 0 to avoid printing visited character $string[$j] = '0'; } } } //Determine minimum and maximum occurring characters $min = $max = $freq[0]; for($i = 0; $i < count($freq); $i++) { //If min is greater than frequency of a character //then, store frequency in min and corresponding character in minChar if($min > $freq[$i] && $freq[$i] != '0') { $min = $freq[$i]; $minChar = $string[$i]; } //If max is less than frequency of a character //then, store frequency in max and corresponding character in maxChar if($max < $freq[$i]) { $max = $freq[$i]; $maxChar = $string[$i]; } } print("Minimum occurring character: " . $minChar); print("<br>Maximum occurring character: " . $maxChar); ?> </body> </html> Output: Minimum occurring character: a Maximum occurring character: e
Next Topic#
|