我正在尝试在给定字符串中返回具有最常见字符的句子中的单词。
例如:
Old man sneezed at Starbucks.
nee
我想在那句话中返回打喷嚏的词。
我刚接触Java,实际上是JS和Python开发人员,所以我对Char数组,ArrayList,StringBuilder等感到非常困惑。
我认为
但是我真的不知道我需要使用什么数组类型。
有人可以帮我解决一些小片段或算法,将其引向解决方案的方法。
这是我的开始方式,但我觉得这不会帮助我完成我所寻找的东西:
ArrayList<String> wordArrayList = new ArrayList<String>();
int count = 0;
for(String eachWord : sentence.split(" ")) {
wordArrayList.add(eachWord);
char[] charArray = eachWord.toCharArray();
char[] givenWord = word.toCharArray();
}
感谢大家的时间和精力。
我想为其添加另一种情况,以阐明我要完成的工作:
Up to dubs
bus
dubs
答案 0 :(得分:1)
假设您只想返回第一个匹配项,则可以使用str.split()并使用str.contains():来修改for循环以遍历句子中的整个单词而不是单个字符。
Verb-Noun
输出:
class Main {
static String findWordThatMatchesSubString(String sentence, String subString) {
for (String word : sentence.split(" ")) {
if (word.contains(subString)) {
return word;
}
}
return null;
}
public static void main(String[] args) {
String sentence = "Old man sneezed at Starbucks.";
String subString = "nee";
String foundWord = findWordThatMatchesSubString(sentence, subString);
if (foundWord != null) {
System.out.println(foundWord);
} else {
System.out.println(subString + " was not found in any word.");
}
}
}
如果您确实需要处理多个比赛,则可以使用ArrayList就像您当前正在做的事情一样合适:
sneezed
输出:
import java.util.ArrayList;
import java.util.List;
class Main {
static List<String> findWordsThatMatchesSubString(String sentence, String subString) {
List<String> wordMatches = new ArrayList<>();
for (String word : sentence.split(" ")) {
if (word.contains(subString)) {
wordMatches.add(word);
}
}
return wordMatches.size() > 0 ? wordMatches : null;
}
public static void main(String[] args) {
String sentence = "Old man sneezed at Starbucks and dropped his knitting needle on the floor.";
String subString = "nee";
List<String> foundWords = findWordsThatMatchesSubString(sentence, subString);
if (foundWords != null) {
System.out.println(foundWords);
} else {
System.out.println(subString + " was not found in any word.");
}
}
}
关于您在句子中找到[sneezed, needle]
中具有所有字符的单词的后续问题,您可以维护Map个字符及其数量以达到所需的结果:
subString
输出:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Main {
static List<String> findWordsThatContainAllCharsInSubString(String sentence, String subString) {
List<String> wordMatches = new ArrayList<>();
for (String word : sentence.split(" ")) {
if (containsAllChars(word, subString)) {
wordMatches.add(word);
}
}
return wordMatches.size() > 0 ? wordMatches : null;
}
static boolean containsAllChars(String word, String subString) {
if (word.length() < subString.length()) {
return false;
}
Map<Character, Integer> subStringCharsMap = new HashMap<>();
for (char c : subString.toCharArray()) {
subStringCharsMap.put(c, subStringCharsMap.getOrDefault(c, 0) + 1);
}
for (char c : word.toCharArray()) {
if (subStringCharsMap.containsKey(c)) {
if (subStringCharsMap.get(c) == 1) {
subStringCharsMap.remove(c);
} else {
subStringCharsMap.put(c, subStringCharsMap.get(c) - 1);
}
}
if (subStringCharsMap.size() == 0) {
return true;
}
}
return false;
}
public static void main(String[] args) {
String sentence = "I got a new pair of shoes";
String subString = "hes";
List<String> foundWords = findWordsThatContainAllCharsInSubString(sentence, subString);
if (foundWords != null) {
System.out.println(foundWords);
} else {
System.out.println(subString + " was not found in any word.");
}
}
}
答案 1 :(得分:0)
您所拥有的是一个良好的开端-拆分句子后,应该有可能对每个单词进行迭代,然后收集包含给定单词的第一个/所有单词。
docker build
然后将返回包含给定单词的所有单词的列表。根据要求,可以将其更改为List<String> wordArrayList = new ArrayList<String>();
String givenWord = // from user
for(String eachWord : sentence.split(" ")) {
if (eachWord.contains(givenWord)) {
wordArrayList.add(eachWord)
}
}
/ startsWith
等。
由于您是Java的新手,所以我假设您没有碰到Stream API的另一种有趣的方式:
endsWith
答案 2 :(得分:0)
您可以只使用字符串类的contains()
方法。它采用一个要在字符串对象中搜索的参数。下面的代码可能会有所帮助。
import java.util.*;
public class Sample{
public static void main(String []args){
ArrayList<String> occurences = new ArrayList<>();
String input = "Old man sneezed at Starbucks.";
String find = "nee";
for(String eachString: input.split(" ")){
if(eachString.contains(find)){
occurences.add(eachString);
}
}
System.out.println(occurences);
}
}