简体   繁体   English

搜索字符串是否相差一个字符

[英]Searching if String differs by one character

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. 用户输入的单词是randomword是文本文件中要搜索的单词。

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,我似乎会得到更多我想要完成的工作,但是然后我的代码仅在前两个字母相同的情况下进行搜索,而在后两个字母相同的情况下进行搜索好吧,应该做的。

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;
}

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"; 例如:如果String1 =“ abc”; and String2 = "zzzabcdef"; 和String2 =“ zzzabcdef”;

You'll need to delete 6 characters from the second string and test for every combination of 6 characters deleted. 您需要从第二个字符串中删除6个字符,并测试删除的6个字符的每个组合。 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. 因此,您需要测试以下字符串: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. 为此,您希望for循环从0到i以及从i + 1到末尾抓取两个子字符串。 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. 这将忽略第ith个字符,并循环查找字符串的大小-1,将首先为您提供完整的字符串,然后为您提供不包含第一个字母的字符串,然后缺少第二个字母,依此类推。 Then test that substring in the same fashion we did above. 然后以与上面相同的方式测试该子字符串。

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; } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM