Java 找出重复字母数最多的单词
对于这个挑战,我需要找到重复字母最多的单词。例如,如果我输入Java 找出重复字母数最多的单词,java,Java,对于这个挑战,我需要找到重复字母最多的单词。例如,如果我输入helloworld输出应该是Hello,因为它包含2个l字符,或者没有单词,并且应该是-1。 我将问题分解为: 1) 将一个句子拆分为单词数组 2) 在循环中遍历每个单词 3) 在循环中遍历每个charcate 如果一个单词包含的字母比其他任何单词都多,我就陷入了该如何返回的困境 public static void main(String[] args) { Scanner kbd = new Scanner(System.
helloworld代码>输出应该是Hello
,因为它包含2个l
字符,或者没有单词,并且应该是-1
。
我将问题分解为:
1) 将一个句子拆分为单词数组
2) 在循环中遍历每个单词
3) 在循环中遍历每个charcate
如果一个单词包含的字母比其他任何单词都多,我就陷入了该如何返回的困境
public static void main(String[] args) {
Scanner kbd = new Scanner(System.in);
System.out.println("Enter any sentence or word combination: ");
String myString = kbd.nextLine();
String result = "";
int count = 0;
String[] words = myString.split("\\s+");
for(int i = 0; i < words.length; i++) {
for(int j = 0; j < words[i].length(); j++) {
for(int k = 1; k < words[i].length(); k++) {
char temp = words[i].charAt(k);
if(temp == words[i].charAt(k-1)) {
count++;
}
}
}
}
}
publicstaticvoidmain(字符串[]args){
扫描仪kbd=新扫描仪(System.in);
System.out.println(“输入任何句子或单词组合:”);
字符串myString=kbd.nextLine();
字符串结果=”;
整数计数=0;
String[]words=myString.split(\\s+);
for(int i=0;i
你几乎做到了,我想你正在研究这样的事情:
static int mostFreqCharCount(final String word) {
final int chars[] = new int[256];
int max = 0;
for (final char c : word.toCharArray()) {
chars[c]++;
if (chars[c] > chars[max]) // find most repetitive symbol in word
max = c;
}
return chars[max];
}
public static void main(final String[] args) {
System.out.println("Enter any sentence or word combination: ");
final Scanner kbd = new Scanner(System.in);
final String myString = kbd.nextLine();
kbd.close();
int maxC = 0;
String result = "";
final String[] words = myString.split("\\s+");
for (final String word : words) {
final int c = mostFreqCharCount(word);
if (c > maxC) {
maxC = c;
result = word;
}
}
if (maxC > 1) // any word has at least 1 symbol, so we should return only 2+
System.out.println(result);
}
主要思想-计算每个单词最常用的符号数,并在变量maxC
和result
中只存储最大的一个,您几乎做到了,我想您正在研究类似的问题:
static int mostFreqCharCount(final String word) {
final int chars[] = new int[256];
int max = 0;
for (final char c : word.toCharArray()) {
chars[c]++;
if (chars[c] > chars[max]) // find most repetitive symbol in word
max = c;
}
return chars[max];
}
public static void main(final String[] args) {
System.out.println("Enter any sentence or word combination: ");
final Scanner kbd = new Scanner(System.in);
final String myString = kbd.nextLine();
kbd.close();
int maxC = 0;
String result = "";
final String[] words = myString.split("\\s+");
for (final String word : words) {
final int c = mostFreqCharCount(word);
if (c > maxC) {
maxC = c;
result = word;
}
}
if (maxC > 1) // any word has at least 1 symbol, so we should return only 2+
System.out.println(result);
}
其主要思想是:计算每个单词最常用的符号数,并在变量maxC
和result
中只存储最大的符号。您需要创建一个length=words.length的数组,并在其相对索引中存储每个单词的最大值:
int counts[] = new int[words.length];
for(int i = 0; i < words.length; i++) {
for(int j = 0; j < words[i].length(); j++){
count = 0
for(int k = k+1; k < words[i].length(); k++){
if(words[i].charAt(j) == words[i].charAt(k)){
count++;
}
if(counts[i] < count)
counts[i] = count;
}
}
int counts[]=新int[words.length];
for(int i=0;i
然后只需在数组中扫描最高值n,并返回单词[n]您将需要创建一个长度=words.length的数组,并将每个单词的最高值存储在其相对索引中:
int counts[] = new int[words.length];
for(int i = 0; i < words.length; i++) {
for(int j = 0; j < words[i].length(); j++){
count = 0
for(int k = k+1; k < words[i].length(); k++){
if(words[i].charAt(j) == words[i].charAt(k)){
count++;
}
if(counts[i] < count)
counts[i] = count;
}
}
int counts[]=新int[words.length];
for(int i=0;i
然后只需扫描数组中的最大值n,并返回单词[n]如果只需要一个具有最大计数的单词,则只需要三个变量,一个用于currentBestWord,一个用于currentLargestCount,另一个用于保持单词中字符的计数
int currentLargestCount=0;
String currentBestWord="";
HashMap<String,Integer> characterCount=new HashMap<String,Integer>();
String[] words=myString.split("\\s+");
for(int i=0;i<words.length;i++){
String w=words[i];
characterCount=new HashMap<String,Integer>();
for(int j=0;j<w.length();j++){
String character=w.charAt(j).toString();
if(characterCount.containsKey(character)){
characterCount.put(character,characterCount.get(character)+1);
}else{
characterCount.put(character,1);
}
}
// Now get the largest count of this word
Iterator ir=characterCount.ValueSet().iterator();
int thiscount=0;
while(ir.hasNext()){
int thischaractercount=ir.next();
if(thiscount<thischaractercount) thiscount=thischaractercount;
}
if(thiscount>currentLargestCount){
currentLargestCount=thiscount;
currentBestWord=w;
}
}
int currentLargestCount=0;
字符串currentBestWord=“”;
HashMap characterCount=新HashMap();
String[]words=myString.split(\\s+);
对于(int i=0;i如果只需要一个具有最大计数的单词,则只需要三个变量,一个用于currentBestWord,一个用于currentLargestCount,一个用于保持单词中字符的计数
int currentLargestCount=0;
String currentBestWord="";
HashMap<String,Integer> characterCount=new HashMap<String,Integer>();
String[] words=myString.split("\\s+");
for(int i=0;i<words.length;i++){
String w=words[i];
characterCount=new HashMap<String,Integer>();
for(int j=0;j<w.length();j++){
String character=w.charAt(j).toString();
if(characterCount.containsKey(character)){
characterCount.put(character,characterCount.get(character)+1);
}else{
characterCount.put(character,1);
}
}
// Now get the largest count of this word
Iterator ir=characterCount.ValueSet().iterator();
int thiscount=0;
while(ir.hasNext()){
int thischaractercount=ir.next();
if(thiscount<thischaractercount) thiscount=thischaractercount;
}
if(thiscount>currentLargestCount){
currentLargestCount=thiscount;
currentBestWord=w;
}
}
int currentLargestCount=0;
字符串currentBestWord=“”;
HashMap characterCount=新HashMap();
String[]words=myString.split(\\s+);
对于(int i=0;你能定义重复字母吗?“helllo”、“heelloo”或“香蕉”的重复计数是多少?值是3吗?我不明白你的k循环在做什么,你不应该从k=j+1开始比较.charAt(j)和.charAt(k)吗
?您还需要将每个单词的count变量归零,可能是每个字母的count变量(取决于您如何定义计数)。@BenjyKessler,它应该返回具有相同首字母重复的单词。在“heelloo”中
,它应该是e
,因为它是第一个。@Tony,是的,正确。你能定义重复的字母吗?helllo,heelloo?或者可能是banana
?的重复计数是多少?值是3吗?我不明白你的k循环在做什么,你不应该从k=j+1开始比较.charAt(j)到.charAt(k)
?您还需要将每个单词的count变量归零,可能是每个字母的count变量(取决于您如何定义计数)。@BenjyKessler,它应该返回具有相同首字母重复的单词。在“heelloo”中
,它应该是e
,因为它是第一个。@Tony,是的,正确。很好!使用256数组而不是哈希映射,非常好cleaner@SashaSalauyou是的,对于完整的java符号集,您需要使用65536长度的arraynice!使用256数组而不是HashMap,非常有用cleaner@SashaSalauyou是的,对于完整的java符号集,您需要使用65536长度aR最好改成Map
最好改成Map