java 搜索字符串是否相差一个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27195890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Searching if String differs by one character
提问by user180708
I'm trying to determine if a word entered differs by one character in a text file. I have code that works, but unfortunately only for words that are two characters or less which obviously isn't very useful, and the code itself looks a bit messy. Here's what I have so far:
我正在尝试确定输入的单词是否与文本文件中的一个字符不同。我有有效的代码,但不幸的是只适用于两个字符或更少的单词,这显然不是很有用,而且代码本身看起来有点乱。这是我到目前为止所拥有的:
if(random.length() == word.length()){
for(int i = 0; i < random.length(); i++){
if( (word.charAt(i) == random.charAt(i))){
str += word+"\n";
count++;
}
}
}
With random
being the word that was entered by the user, and word
being the word to search for in the text file.
随着random
被认为是由用户输入的单词,然后word
是搜索文本文件中的单词。
If I changed my second if
statement to something along the lines of
如果我将第二个if
语句更改为类似以下内容的内容
if( (word.charAt(i) == random.charAt(i)) && (word.charAt(i -1) == random.charAt(i-1)))
and if I change int i
to be = 1 instead, I seem to get more of what I'm looking to accomplish, but then my code is searching for only if the first two letters are the same and not if the last two are as well, which it should be doing.
如果我改为int i
= 1,我似乎得到了更多我想要完成的东西,但是我的代码仅在前两个字母相同而不是后两个字母相同时才搜索,它应该做什么。
回答by luuksen
I assume you need a function like this? I just wrote and tested it.
我假设您需要这样的功能?我刚刚编写并测试了它。
static boolean equals(String word1, String word2, int mistakesAllowed) {
if(word1.equals(word2)) // if word1 equals word2, we can always return true
return true;
if(word1.length() == word2.length()) { // if word1 is as long as word 2
for(int i = 0; i < word1.length(); i++) { // go from first to last character index the words
if(word1.charAt(i) != word2.charAt(i)) { // if this character from word 1 does not equal the character from word 2
mistakesAllowed--; // reduce one mistake allowed
if(mistakesAllowed < 0) { // and if you have more mistakes than allowed
return false; // return false
}
}
}
}
return true;
}
回答by Matt C
Your code seems to be working to me, you just may be interpreting its results incorrectly.
您的代码似乎对我有用,您可能只是错误地解释了它的结果。
This may be more obvious:
这可能更明显:
int count = 0; if(random.length() == word.length()) {
for(int i = 0; i < random.length(); i++)
{
if( (word.charAt(i) != random.charAt(i) ))
{
if(count == 0)
{
System.out.println("Found first difference!");
}
if(count != 0)
{
System.out.println("Strings are more than one letter different!");
}
count++;
}
} }
If you want to check Strings of different lengths, you'll need to delete characters from the longer one until it's the same size as the shorter. For example: If String1 = "abc"; and String2 = "zzzabcdef";
如果要检查不同长度的字符串,则需要从较长的字符串中删除字符,直到与较短的字符串大小相同。例如: If String1 = "abc"; 和 String2 = "zzzabcdef";
You'll need to delete 6 characters from the second string and test for every combination of 6 characters deleted. So you would want to test the strings: def, cde, abc, zab, zza, zzz, zzb, zzc, zzd, zze, zzf, zaf, zae, zad, zac, zab, zza, zzf, zze, ..., ..., on and on, the list is of size 9 choose 6, so it's definitely not optimal or recommended.
您需要从第二个字符串中删除 6 个字符并测试每个删除的 6 个字符的组合。所以你会想要测试字符串:def、cde、abc、zab、zza、zzz、zzb、zzc、zzd、zze、zzf、zaf、zae、zad、zac、zab、zza、zzf、zze、... , ..., 不断地,该列表的大小为 9 选择 6,因此它绝对不是最佳或推荐的。
You can however, check to see if a string which is one character longer than the other is just the other string with one added letter. To do this, you want a for loop to grab two substring from 0 to i, and from i+1 to the end. This will leave out the ith character, and looping for the size of the string - 1, will give you first the full string, then the string without the first letter, then missing the second letter, and so on. Then test that substring in the same fashion we did above.
但是,您可以检查一个字符长于另一个字符的字符串是否只是添加了一个字母的另一个字符串。为此,您需要一个 for 循环来抓取从 0 到 i 以及从 i+1 到结尾的两个子字符串。这将省略第 i 个字符,并循环字符串的大小 - 1,将首先提供完整的字符串,然后是没有第一个字母的字符串,然后缺少第二个字母,依此类推。然后以与上面相同的方式测试该子字符串。
Comment if this was not what you're looking for.
如果这不是您要查找的内容,请发表评论。
EDIT
编辑
To see how many words in a file are one letter different than a variable word, you need to loop through the file, getting each word. Then testing if that was string was one letter off. It would be something like this:
要查看文件中有多少单词与可变单词不同一个字母,您需要遍历文件,获取每个单词。然后测试那是否是字符串是一个字母。它会是这样的:
String testAgainst = "lookingForWordsOneLetterDifferentThanThisString";
int words = 0;
Scanner scan = new Scanner(fileName);
while(scan.hasNext())
{
String word = scan.next();
if( isOneDifferent(word, testAgainst) )
{
words++;
}
System.out.println("Number of words one letter different: " + words);
}
public boolean isOneDifferent(String word, String testAgainst)
{
if(word.length() != testAgainst.length())
{
return false;
}
int diffs = 0;
for(int i = 0; i < word.length(); i++)
{
if(word.charAt(i) != testAgainst.charAt(i))
{
diffs++;
}
if(diffs > 1)
{
return false;
}
}
if(diffs == 1)
{
return true;
}
else
{
return false;
}
}