此方法的目的是,当我给它一个字符串作为输入时,它必须向我返回出现频率最高的字母。
如果我给这样的字符串作为输入,它将正常工作:
String text = "helllo";
所以它给了我
The most frequent letter is l with: 2 occurrences
是正确的,但是如果我将其作为输入
String text = "abbccdd";
它给了我
The most frequent letters are b,d with: 2 occurrences
这是不正确的,因为它必须给我
The most frequent letters are b,c,d with: 2 occurences
请帮助!
BusinessLogic.java
package com.mycompany.showcharwithhighestoccurrence.javafxBusinessLogic;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class BusinessLogic {
public void hello() {
System.out.println("helllllo");
}
public String giveFinalResult(String text) {
String input = " ";
String output = " ";
if (text != null) {
input = text.toLowerCase().replaceAll("\\s+", "");
int[] freq = new int[input.length()];
int i, j, max, lastFreq;
lastFreq = 0;
//Converts given string into character array
char inputChars[] = input.toCharArray();
List<Character> maxChars = new ArrayList<>();
//Count each word in given string and store in array freq
for (i = 0; i < inputChars.length; i++) {
freq[i] = 1;
for (j = i + 1; j < inputChars.length; j++) {
if (inputChars[i] == inputChars[j] && inputChars[i] != ' ' && inputChars[i] != '0') {
freq[i]++;
inputChars[j] = '0';
}
}
}
//Determine maximum occurring characters
if (freq.length > 0) {
lastFreq = freq[0];
}
for (i = 0; i < freq.length; i++) {
max = freq[0];
if (freq[i] == lastFreq) {
max = lastFreq;
}
if (max == lastFreq && max < freq[i]) {
lastFreq = freq[i];
maxChars.add(inputChars[i]);
for (Character c : maxChars) {
if (c != null) {
output = "The most frequent letter is " + c + " with: " + freq[i] + "occurrences";
}
}
}
if (lastFreq < freq[i]) {
maxChars.clear();
maxChars.add(inputChars[i]);
for (Character c : maxChars) {
if (c != null) {
output = "The most frequent letter is " + c + " with: " + freq[i] + "occurrences";
}
}
}
if (max > 1 && max == freq[i]) {
maxChars.add(inputChars[i]);
output = "The most frequent letter are " + maxChars.get(0).toString() + ", "
+ iterator.next().toString() + " with: " + freq[i] + "occurrences";
}
}
}
}
return output;
}
}
答案 0 :(得分:3)
为什么这么复杂?
您只需要3个简单步骤:
// collect frequencies
Map<String, Long> collect = "abbccdde".chars()
.mapToObj(c -> Character.toString((char) c))
.collect(Collectors.groupingBy(x -> x, Collectors.counting()));
// find max
long max = collect.values().stream().max(Comparator.naturalOrder()).get();
//filter values for max
String result = collect.entrySet()
.stream()
.filter(x -> x.getValue() == max)
.map(Map.Entry::getKey)
.collect(Collectors.joining(","));
String output = "The most frequent letter are " + result + " with: " + max + "occurrences";
System.out.println("output = " + output);
答案 1 :(得分:1)
完全重写:
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class Test {
public static void main(String[] args) {
System.out.println(giveFinalResult("hello"));
System.out.println(giveFinalResult("abbccdd"));
System.out.println(giveFinalResult("hello world!"));
}
public static String giveFinalResult(String text) {
int maxFreq = 0;
List<Character> listCharMaxFreq = new LinkedList<>();
Map<Character, Integer> counts = new HashMap<>();
// compute occurence of each characters
for(char c : text.toCharArray()) { // prefer foreach loop
Integer i = counts.get(c);
if(i == null) { // character not yet met
i = 1; // so 1
} else {
++i; // otherwise, increment
}
counts.put(c, i); // save new count
// keep trace of most occurred chars
if(i >= maxFreq) {
if(i > maxFreq) {
listCharMaxFreq.clear();
}
maxFreq = i;
listCharMaxFreq.add(c);
}
}
// construct output string
StringBuilder sb = new StringBuilder();
sb.append("In ")
.append(text)
.append(", the most frequent letter is ")
.append(listCharMaxFreq)
.append(" with ")
.append(maxFreq)
.append(" occurrences.");
return sb.toString();
}
}
您应该考虑将此方法分为两部分。首先,计算并返回更频繁出现的字符列表。其次,构造输出String。这样,您可以轻松地重用第一种方法。