在Java中,如何在字符数组中查找某个单词的字母?
我需要比较两个不同字符数组中的字符,以找到用户输入的隐藏字 目标是输入两个字符串,并在另一个字符串中找到一个被置乱的单词。 例如,“tot”一词在“西红柿”一词中加了一层 在论坛的一些人的帮助下,我实现了字符数组来存储用户字符串,但我不知道如何检查每个数组中所需的字符。我尝试了下面的代码,但它总是导致程序无法找到单词。如果有人能提供更好的方法或解决方案,我将不胜感激在Java中,如何在字符数组中查找某个单词的字母?,java,arrays,string,loops,compare,Java,Arrays,String,Loops,Compare,我需要比较两个不同字符数组中的字符,以找到用户输入的隐藏字 目标是输入两个字符串,并在另一个字符串中找到一个被置乱的单词。 例如,“tot”一词在“西红柿”一词中加了一层 在论坛的一些人的帮助下,我实现了字符数组来存储用户字符串,但我不知道如何检查每个数组中所需的字符。我尝试了下面的代码,但它总是导致程序无法找到单词。如果有人能提供更好的方法或解决方案,我将不胜感激 public static void main(String[] args) { input = new Scanner(
public static void main(String[] args) {
input = new Scanner(System.in);
System.out.println("Please enter a word");
String word = input.next();
char[] charOfWrds = word.toCharArray();
System.out.println("Please enter a hidden word you would like to search for");
String search = input.next();
char[] charOfSrch = search.toCharArray();
if (isContains(charOfWrds, charOfSrch))
{
System.out.print("The word " + search + " is found in the word " + word);
}
else
{
System.out.print("The word was not found in " + word);
}
}
public static Boolean isContains(char[] charOfWrds, char[] charOfSrch) {
int count = 0;
for (char cha : charOfWrds)
{
for (char chaaa : charOfSrch)
{
if (cha == chaaa)
count++;
}
}
if (count == charOfSrch.length)
{
return true;
}
return false;
}
接受
西红柿
tot as charOfSrch
iContains将计数为6,因为当您在第二个单词中找到第一个单词的字母时,您不会退出
t:两次
o:两次
t:两次
试试这个:
if (cha == chaaa){
count++;
break;
}
但是为了实现这一点,您需要删除第二个字符串中的字母,因为如果您要查找的单词是“tttt”,那么即使不是,该代码也会为您提供true,但是如果您在找到它时删除了t,那么它应该会起到作用
我不知道这对你来说是否足够清楚
代码如下:
public static Boolean isContains(char[] charOfWrds, char[] charOfSrch) {
int count = 0;
for (int i=0; i<charOfWrds.length;i++){
for (int j=0;j<charOfSrch.length;j++){
if (charOfWrds[i] == charOfSrch[j]){
count++;
charOfSrch[j]=' ';
break;
}
}
}
if (count == charOfSrch.length)
{
return true;
}
return false;
}
但是这很难看,你应该尝试使用java Api,除非你真的必须使用数组。public static Boolean isContains(char[]charOfWords,char[]charofsch){
public static Boolean isContains(char[] charOfWords, char[] charOfSrch) {
List<Character> searchFor = new ArrayList<>();
List<Character> searchMe=new ArrayList<>();
for(char c:charOfWords)
searchFor.add(c);
for(char c:charOfSrch)
searchMe.add(c);
for(int x=searchFor.size()-1;x>=0;x--){
if(searchMe.contains(searchFor.get(x)){
searchMe.remove(searchFor.get(x));
searchFor.remove(x);//line A
}
}
return searchFor.size()==0;
}
List searchFor=new ArrayList();
List searchMe=new ArrayList();
for(char c:charOfWords)
搜索。添加(c);
for(char c:charofsch)
加上(c);
对于(int x=searchFor.size()-1;x>=0;x--){
if(searchMe.contains(searchFor.get(x)){
searchMe.remove(searchFor.get(x));
searchFor.remove(x);//行A
}
}
返回searchFor.size()==0;
}
快速概述这项功能。我将两个字符数组都转换为列表,以便使用列表方法。然后,我迭代了单词中需要查找的每个字符,如果我可以在另一个单词中找到,我将其从两个字符中删除,这意味着如果从单词中删除所有单词,则需要使用find,这个词在另一个词中找到,否则就不是了
我假设您不能重复使用第二个单词中的字母,但如果可以,请删除A行。您应该尝试使用正则表达式,而不是尝试编写算法。
使用用户输入构建表达式,然后与所需单词匹配。我的想法与@kirbyquerby提供的类似,只是有一些优化 在将每个单词(针和草堆)转换成一个列表后,我们对这些列表进行排序,而不是使用线性搜索。这允许我们使用二进制搜索,将搜索复杂度从O(n^2)更改为O(n log n) 此外,无需从针列表中删除字符,因为我们只需跟踪找到了多少针字符。一旦我们完成搜索,我们只需将找到的针字符数与针字符总数进行比较。如果它们相等,我们就找到了针。最后,如果针字符如果没有找到针,我们可以立即停止搜索,因为我们知道整个针不存在于草堆中
package hiddenword;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class HiddenWord {
public static void main(final String args[]) {
final String haystack = "Tomato";
final String needle = "tot";
if (contains(haystack, needle)) {
System.out.println(haystack + " contains " + needle);
} else {
System.out.println(haystack + " does not contain " + needle);
}
}
private static boolean contains(final String haystack, final String needle) {
// Convert each word to lowercase and into a List.
final List<Character> haystackChars = toSortedCharacterList(haystack.toLowerCase());
final List<Character> needleChars = toSortedCharacterList(needle.toLowerCase());
int foundNeedleChars = 0;
for (final Character c : needleChars) {
// Using sorted lists, our search time is be O(n log n) by using
// binary search instead of O(n^2) using linear search.
int index = Collections.binarySearch(haystackChars, c);
// A needle character has been found, remove it from the haystack
// and increment the count
if (index >= 0) {
haystackChars.remove(index);
++foundNeedleChars;
}
// A needle character was not found. This means that the haystack
// doesn't contain every character of the needle and we
// can quit early
else {
return false;
}
}
// If we've found all the needle characters, the needle word exists in
// the haystack word.
return foundNeedleChars == needleChars.size();
}
private static List<Character> toSortedCharacterList(final String input) {
final List<Character> list = new ArrayList<Character>();
// Convert primitive array to List
for (final char c : input.toCharArray()) {
list.add(c);
}
// Sort that thang
Collections.sort(list);
return list;
}
}
包隐藏字;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
公共类隐藏字{
公共静态void main(最终字符串参数[]){
最终字符串haystack=“番茄”;
最后一根线针=“tot”;
if(包含(干草堆、针)){
System.out.println(haystack+“包含”+针);
}否则{
System.out.println(haystack+“不包含”+针);
}
}
私有静态布尔包含(最终字符串草堆、最终字符串指针){
//将每个单词转换为小写字母并转换为列表。
最终列表haystackChars=toSortedCharacterList(haystack.toLowerCase());
最终列表needChars=toSortedCharacterList(needle.toLowerCase());
int-foundNeedleChars=0;
for(最后一个字符c:Neederchars){
//使用排序列表,我们的搜索时间是O(n logn)
//使用线性搜索代替O(n^2)的二进制搜索。
int index=Collections.binarySearch(haystackChars,c);
//发现一个针形字符,请将其从干草堆中移除
//并增加计数
如果(索引>=0){
移除(索引);
++铸造针孔;
}
//未找到针字符。这意味着干草堆
//不包含针的所有字符,我们
//你能早点退出吗
否则{
返回false;
}
}
//如果我们找到了所有的针形字符,那么针形词就存在于
//干草堆这个词。
return foundNeedleChars==needleChars.size();
}
私有静态列表到SortedCharacterList(最终字符串输入){
最终列表=新的ArrayList();
//将基元数组转换为列表
for(最终字符c:input.toCharArray()){
增加(c)项;
}
//把那个排序
集合。排序(列表);
退货清单;
}
}
您无需将字符串转换为数组即可执行此操作,例如:
static boolean containsScrambled(String word, String search){
//loop through each character of the word whose characters we are searching for
for(int x = 0; x<search.length(); x++){
//find the current character to check
String c = search.substring(x, x+1);
if(word.indexOf(c) >= 0){
//if the character is in the word, remove the first instance of it
//as we cannot use that character again
word.replaceFirst(c, "");
}else{
//if the character is not in the word, fail and return false
return false;
}
}
//if we made it here, all of the characters existed, return true
return true;
}
static boolean containsScrambled(字符串字、字符串搜索){
//循环遍历我们正在搜索的单词的每个字符
对于(int x=0;x=0){
//如果字符在单词中,请删除它的第一个实例
//因为我们不能再使用那个字符了
字。替换第一(c,“”);
}否则{
//如果字符不在单词中,请失败并重试
static boolean containsScrambled(String word, String search){
//loop through each character of the word whose characters we are searching for
for(int x = 0; x<search.length(); x++){
//find the current character to check
String c = search.substring(x, x+1);
if(word.indexOf(c) >= 0){
//if the character is in the word, remove the first instance of it
//as we cannot use that character again
word.replaceFirst(c, "");
}else{
//if the character is not in the word, fail and return false
return false;
}
}
//if we made it here, all of the characters existed, return true
return true;
}