Suppose I have a txt file with strings {ABAA, AAAA, ABZA, ABZZ, and AAZZ} and my Start word is AAAA and my end word is AAZZ. I need to find all the words between the start word and end word different by one character; so from the example given my results would be: AAAA, ABZZ and 开发者_如何学JAVAAAZZ.
At the moment what I am doing is creating a list and reading the file line-by-line and passing it to the list.
// 1 Declare new List.
List<string> lines = new List<string>();
// 2
// Use using StreamReader for disposing.
using (StreamReader sr = new StreamReader(PATH))
{
// 3
// Use while != null pattern for loop
string line;
while ((line = sr.ReadLine()) != null)
{
// 4
// Insert logic here.
// ...
// "line" is a line in the file. Add it to our List.
lines.Add(line);
}
}
My question is: how do I look for strings different by one character? Do I need to break the string that I read from the file into characters and do a comparison to my Start and End Strings?
bool compareStrings(a, b): return a.Zip(b, (a,b) => { a, b }).Where(x => x.a != x.b).Take(2).Count() <= 1;
Regular expressions are very good at finding this sort of thing and .NET has excellent support for regular expressions. First you need to define the regular expression. Your requirements are a bit vague but according to your description, example data and example results I'm inferring that you want to match the start word and every word that varies from the end word by exactly one character. The regex you need is:
\bAAAA\b|\bAAZ\w\b|\bAA\wZ\b|\bA\wZZ\b|\b\wAZZ\b
Let me break that down for left to right.
- '\b' means "word boundary" which could be whitespace or a curly brace or other such non-word character.
- 'AAAA' is your start word and would be matched litterally
- '\b' means "word boundary"
- '|' means "alternation" which essentially means "match the expression on the left OR match the expression on the right"
- '\b' means "word boundary"
- 'AAZ\w' is the first permutation of one-character differences from your end word. '\w' means "any word character."
- '\b' means "word boundary"
- '\bAA\wZ\b' is the second permutation of one-character differences from your end word.
- '\bA\wZZ\b' is the third permutation.
- '\b\wAZZ\b' is the fourth and final permutation and would also match the end word.
See http://www.regular-expressions.info/reference.html for definitions of "word boundary" and "word character."
Now for the code:
using System;
using System.Text.RegularExpressions;
string pattern = @"\bAAAA\b|\bAAZ\w\b|\bAA\wZ\b|\bA\wZZ\b|\b\wAZZ\b";
// 1 Declare new List.
List<string> lines = new List<string>();
// 2
// Use using StreamReader for disposing.
using (StreamReader sr = new StreamReader(PATH))
{
// 3
// Use while != null pattern for loop
string line;
while ((line = sr.ReadLine()) != null)
{
// 4
if (Regex.IsMatch(line, pattern, RegexOptions.IgnoreCase))
{
// ...
// "line" is a line in the file. Add it to our List.
lines.Add(line);
}
}
}
I'm not sure of all the requirements, but this function should return the amount of characters that match between two words.
private int CheckWord(string startWord, string otherWord)
{
List<char> start = new List<char>(startWord.ToArray());
List<char> wordt = new List<char>(otherWord.ToArray());
return start.Intersect(wordt).Count();
}
This call CheckWord("start", "srart"); returns 4. Match that number against the length of the string to determine how different they are.
精彩评论