Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

find the maximum and minimum occurring character in string

The document outlines a method to find the maximum and minimum occurring characters in a string using both a nested loop approach and a more efficient HashMap approach. The first method has a time complexity of O(n^2), while the HashMap method improves it to O(n). It also discusses the space complexity of O(n) due to the HashMap used for storing frequencies.

Uploaded by

Kaushal Bhatt
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

find the maximum and minimum occurring character in string

The document outlines a method to find the maximum and minimum occurring characters in a string using both a nested loop approach and a more efficient HashMap approach. The first method has a time complexity of O(n^2), while the HashMap method improves it to O(n). It also discusses the space complexity of O(n) due to the HashMap used for storing frequencies.

Uploaded by

Kaushal Bhatt
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

find the maximum and minimum occurring character in a string

Start

Declare a string

Ask the user to initialize it.

Declare an array to store the frequency of the elements.

Declare variables for minimum and maximum occurring characters and assign them to
0.

Convert the string to a character array.

Use two for loops for the same.

Use the first for loop to iterate through each character in the string.

Select the characters and initialize their corresponding frequency in the frequency
array to 1.

Use the second for loop to compare the selected character with the rest of the
characters present in the string.

Again use two for loops to iterate over the frequency array.

Use the min and max variable to store the count of minimum and maximum occurring
characters in the string.

Iterate over the loop and compare the count of each character stored in freq with
min and max.

If the count stored in freq is less than the value of min, then store that count in
the min and corresponding character in minChar.

If the count stored in freq is more than the value of max, then store that count in
max and corresponding character in maxChar.

Now, the minChar will store the minimum occurring character and maxChar will store
the maximum occurring character.

Print the minimum and maximum occurring characters.

Stop

import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Take input from the user
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string: ");
String str=sc.nextLine();
int[] freq = new int[str.length()];
char minChar = str.charAt(0), maxChar = str.charAt(0);
int i, j, min, max;
char string[] = str.toCharArray();
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]++;

string[j] = '0';
}
}
}
min = max = freq[0];
for(i = 0; i <freq.length; i++)
{
if(min > freq[i] && freq[i] != '0')
{
min = freq[i];
minChar = string[i];
}
if(max < freq[i])
{
max = freq[i];
maxChar = string[i];
}
}
System.out.println("Minimum occurring character: " + minChar);
System.out.println("Maximum occurring character: " + maxChar);
}
}

###################################################################################
#############

A better solution can be achieved using a HashMap to store the frequency of each
character. This way, you can avoid the nested loop and reduce the time complexity
to \(O(n)\).

Here's a Java code snippet that demonstrates this improved approach:

```java
import java.util.HashMap;
import java.util.Map;

public class Main {


public static void main(String[] args) {
String str = "Example of minimum and maximum Character";
System.out.println("The entered string is: " + str);

Map<Character, Integer> freqMap = new HashMap<>();

// Populate frequency map


for (char c : str.toCharArray()) {
if (c != ' ') {
freqMap.put(c, freqMap.getOrDefault(c, 0) + 1);
}
}

// Initialize min and max frequency characters


char minChar = str.charAt(0), maxChar = str.charAt(0);
int minFreq = Integer.MAX_VALUE, maxFreq = Integer.MIN_VALUE;

// Find the min and max frequency characters


for (Map.Entry<Character, Integer> entry : freqMap.entrySet()) {
int freq = entry.getValue();
char c = entry.getKey();

if (freq < minFreq) {


minFreq = freq;
minChar = c;
}

if (freq > maxFreq) {


maxFreq = freq;
maxChar = c;
}
}

System.out.println("Minimum occurring character: " + minChar);


System.out.println("Maximum occurring character: " + maxChar);
}
}
```

### Time Complexity Analysis:

1. Iterating over the string to populate the HashMap takes \( O(n) \) time.
2. Iterating over the HashMap entries to find the min and max frequency characters
also takes \( O(n) \) time.

So, the overall time complexity is \( O(n) + O(n) = O(n) \).

### Space Complexity:

The space complexity remains \( O(n) \) due to the HashMap used for storing
frequencies.

### Why This Approach?

This approach is more efficient for large strings compared to the previous \
( O(n^2) \) approach. It uses a HashMap to store character frequencies in one pass,
making it easier to find the minimum and maximum frequency characters.

You might also like