Java程序查找出现在单词中最多的字母?
我有一个句子,我想找到出现在大多数单词中的字符,以及它出现在多少个单词中。 例如:“我喜欢拜访住在佛罗里达州奥兰多的朋友威尔。” 它应该输出Java程序查找出现在单词中最多的字母?,java,Java,我有一个句子,我想找到出现在大多数单词中的字符,以及它出现在多少个单词中。 例如:“我喜欢拜访住在佛罗里达州奥兰多的朋友威尔。” 它应该输出i8。 这是我的代码: char maxChar2 = '\0'; int maxCount2 = 1; for (int j=0; j<strs2.length; j++) { int charCount = 1; char localChar = '\0';
i8
。
这是我的代码:
char maxChar2 = '\0';
int maxCount2 = 1;
for (int j=0; j<strs2.length; j++) {
int charCount = 1;
char localChar = '\0';
for (int k=0; k<strs2[j].length(); k++) {
if (strs2[j].charAt(k) != ' ' && strs2[j].charAt(k) != maxChar2) {
for (int l=k+1; l<strs2[j].length(); l++) {
if (strs2[j].charAt(k)==strs2[j].charAt(l)) {
localChar = strs2[j].charAt(k);
charCount++;
}
}
}
}
if (charCount > maxCount2) {
maxCount2 = charCount;
maxChar2 = localChar;
}
}
charmaxchar2='\0';
int maxCount2=1;
对于(intj=0;j,这里有一个更优雅的解决方案
public static void FindMostPopularCharacter(String input)
{
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
input = input.toUpperCase();
HashMap<Character, Integer> charData = new HashMap<>();
char occursTheMost = 'A'; //start with default most popular char
int maxCount = 0;
//create the map to store counts of all the chars seen
for(int i = 0; i < alphabet.length(); i++)
charData.put(alphabet.charAt(i), 0);
//first find the character to look for
for(int i = 0; i < input.length(); i++)
{
char c = input.charAt(i);
//if contained in our map increment its count
if(charData.containsKey(c))
charData.put(c, charData.get(c) + 1);
//check for a max count and set the values accordingly
if(charData.containsKey(c) && charData.get(c) > maxCount)
{
occursTheMost = c;
maxCount = charData.get(c);
}
}
//final step
//now split it up into words and search which contain our most popular character
String[] words = input.split(" ");
int wordCount = 0;
CharSequence charSequence;
for(Character character : charData.keySet())
{
int tempCount = 0;
charSequence = "" + character;
for(int i = 0; i < words.length; i++)
{
if(words[i].contains(charSequence))
tempCount++;
}
if(tempCount > wordCount)
{
occursTheMost = character;
wordCount = tempCount;
}
}
System.out.println(occursTheMost + " " + wordCount);
}
是
注意:如果存在连接,则仅输出首次达到最大出现次数的字符
FindMostPopularCharacter("aabb aabb aabb bbaa");
输出
B 4
因为由于输入中的最后一个字,B在A之前先达到最大值
FindMostPopularCharacter("aab aab b")
B 3
作为提示,尝试使用更有意义的变量名和适当的缩进。这将非常有帮助,尤其是当你的程序没有完成你认为它应该做的事情时。同样,开始更小的程序并为它编写一些测试也会有帮助。与其用一个完整的句子,不如用2个单词,然后3个单词,然后是一个更复杂的句子
重写代码使其更具可读性:
// Where sentence is: "I like".split(" ");
private static void getMostFrequentLetter(String[] sentence) {
char mostFrequentLetter = '\0';
int mostFrequentLetterCount = 1;
for (String word : sentence) {
int charCount = 1;
char localChar = '\0';
for (int wordIndex = 0; wordIndex < word.length(); wordIndex++) {
char currentLetter = word.charAt(wordIndex);
if (currentLetter != ' ' && currentLetter != mostFrequentLetter) {
for (int l = wordIndex + 1; l < word.length(); l++) {
char nextLetter = word.charAt(l);
if (currentLetter == nextLetter) {
localChar = currentLetter;
charCount++;
}
}
}
}
if (charCount > mostFrequentLetterCount) {
mostFrequentLetterCount = charCount;
mostFrequentLetter = localChar;
}
}
}
//其中的句子是:“我喜欢”。拆分(“”);
私有静态void getMostFrequentLetter(字符串[]句){
char mostFrequentLetter='\0';
int-mostFrequentLetterCount=1;
for(字符串:句子){
int charCount=1;
char localChar='\0';
for(int-wordIndex=0;wordIndexmostFrequentLetterCount){
mostFrequentLetterCount=字符数;
mostFrequentLetter=localChar;
}
}
}
现在,我所做的只是重命名变量,并将for循环更改为a for each循环。通过这样做,您可以更清楚地看到您的算法和您正在尝试执行的操作。基本上,您要遍历每个单词,并将当前字母与下一个字母进行比较,以检查是否重复。如果我使用“我喜欢”运行此操作我应该得到i 2
,但我得到的是null char 1
。您没有正确地比较和保存常用字母。这并没有给您答案,但我希望这能让您更清楚地了解您的代码在做什么,以便您可以修复它。问我们推荐或查找书籍、工具、软件库、教程或其他非现场resource与堆栈溢出无关,因为它们往往会吸引固执己见的答案和垃圾邮件。相反,请描述问题以及迄今为止为解决问题所做的工作。输出应该是i8
,而不是i7
。其中有8个单词带有I
:I,like,visting,friend,Will,lives,in,Florida。这段代码证明了这一点:If输入是文本我喜欢拜访我的朋友威尔,他住在佛罗里达州奥兰多。
,那么字符串数组是什么呢strs2
?文本只是一个字符串,而不是字符串数组。@JohnDoe您当前接受的生成,并且无法调整算法以生成正确的结果。一个完全不同的算法hm是必需的,因此您可能需要重新考虑接受该答案,因为它会误导其他人寻找有效的解决方案。结果不正确。FindMostPopularCharacter(“aab aab”)
打印出A2
,但答案是B3
。你的全部前提是大多数出现的字母也出现在大多数单词中。这被证明是一个错误的假设,因此代码无法产生正确的结果。@Andreas很好,我误解了,以为他只想找出哪个字符是被发现的句子中的大多数以及在句子中找到的单词数量。
FindMostPopularCharacter("aab aab b")
B 3
// Where sentence is: "I like".split(" ");
private static void getMostFrequentLetter(String[] sentence) {
char mostFrequentLetter = '\0';
int mostFrequentLetterCount = 1;
for (String word : sentence) {
int charCount = 1;
char localChar = '\0';
for (int wordIndex = 0; wordIndex < word.length(); wordIndex++) {
char currentLetter = word.charAt(wordIndex);
if (currentLetter != ' ' && currentLetter != mostFrequentLetter) {
for (int l = wordIndex + 1; l < word.length(); l++) {
char nextLetter = word.charAt(l);
if (currentLetter == nextLetter) {
localChar = currentLetter;
charCount++;
}
}
}
}
if (charCount > mostFrequentLetterCount) {
mostFrequentLetterCount = charCount;
mostFrequentLetter = localChar;
}
}
}