Reputation: 81
I have to find the char which appears the least in a string.
package String2;
public class Methods3 {
static final int chars=256;
static char least(String str) {
int[] bro=new int[chars];
int j;
//I made the frequency of chars:
for(j=0 ; j<str.length() ; j++) {
(bro[str.charAt(j)])++; //made the array
}
int min= bro[str.charAt(0)];
//Tried finding the smallest value:
for(int x=0 ; x<bro.length ; x++ ) {
if((bro[str.charAt(x)])<=min) {
min=(bro[str.charAt(x)]); //finding the smallest number of times
}
}
return (char) min;
}
public static void main(String[] args) {
String txt="yooooo bbaa ccoo";
char rez=least(txt);
System.out.println(rez);
}
}
Upvotes: 0
Views: 113
Reputation: 116
The Problem in your code is in:
bro[str.charAt(x)])
As the bro
array is 256 long, x is also in the range of [0,256], but in your case you are indexing into the string str
itself, which is not 256 long.
Also you are returning the min number of occurrences as a character, and not the character itself, in this case you have to return the key from the array not the value, in this case you need to return x
.
Edited: You must also ignore all the character that don't appear at all in the string, in which case would always have a value of 0 and always be the min, despite not occurring.
Replace:
for(int x = 0; x < bro.length; x++) {
if ((bro[str.charAt(x)]) <= min) {
min = (bro[str.charAt(x)]); // finding the smallest number of times
}
}
return (char) min;
with:
for (int x = 0; x < bro.length; x++) {
if ((bro[x]) <= min && bro[x] > 0) {
min = x; // finding the smallest number of times
}
}
return (char) min;
Upvotes: 2
Reputation: 1863
I guess you wanted to print "y", and your code is broken in many ways:
this fixes it a bit:
public class Methods3 {
static final int chars = Character.MAX_VALUE;
static char least(String str) {
int[] bro = new int[chars];
int j;
for (j = 0; j < str.length(); j++) {
(bro[str.charAt(j)])++; // made the array
}
int min = Character.MAX_VALUE;
for (int x = 0; x < bro.length; x++) {
if ((bro[x]) <= min && bro[x] > 0) {
min = x; // finding the smallest number of times
}
}
return (char) min;
}
public static void main(String[] args) {
String txt = "yooooo bbaa ccoo";
char rez = least(txt);
System.out.println(rez);
}
}
(fixed to use Character.MAX_VALUE as suggested). This will work with any string.
Please note that min must be initialized with the maximum value for your algorithm to work, hence min=Character.MAX_VALUE.
Where you used "bro[str.charAt(x)]" you had to use "bro[x]".
And in the if you need an extra condition "bro[x] > 0" to ignore chars you did not find.
Also if you have two letters with the same count it will print the last to appear in the string with the same minimum count.
Upvotes: 1
Reputation: 2436
String txt="yooooo bbaa ccoo"; // string length here
your string length index is in the range 0 <= x <= str.length -1
where as your for loop range is 0 <= x <= 255
.
in the for loop, 0 <= x <= 255
, see str.charAt(x)
if((bro[str.charAt(x)])<=min)
however,
str.length < 256 // that is the problem
x
should be 0 <= x <= str.length -1
Upvotes: 0
Reputation: 1545
minOccurences
with total number of charsminOccurences
is greater than current number set it to current number, clear minChars
and add the charminOccurences
is equal to current number add the charUpvotes: 0
Reputation: 1442
Strings in Java are not one byte per symbol. Try to create bro
array with length of Character.MAX_VALUE
or use HashMap instead of array.
int[] bro=new int[Character.MAX_VALUE];
With Map
Map bro = new HashMap();
for(char cr : str.toCharArray()){
bro.compute(cr, (key, val) -> val == null : 1 : val + 1);
}
Upvotes: 2