C# 在文本中查找查询的匹配项
这个问题最近在面试中被问到,我无法解决,所以需要一些建议我如何解决这个问题 声明:我不能使用正则表达式或任何内置库 *****问题陈述如下*********C# 在文本中查找查询的匹配项,c#,string,algorithm,data-structures,C#,String,Algorithm,Data Structures,这个问题最近在面试中被问到,我无法解决,所以需要一些建议我如何解决这个问题 声明:我不能使用正则表达式或任何内置库 *****问题陈述如下********* public class StringPatternMatch { public static bool MatchPattern(string inputText, string pattern) { int count = 0; int patternIndex = 0; for (va
public class StringPatternMatch
{
public static bool MatchPattern(string inputText, string pattern)
{
int count = 0; int patternIndex = 0;
for (var i = 0; i < inputText.Length; i++)
{
if (patternIndex > pattern.Length)
break;
if (inputText[i] == pattern[patternIndex] ||
(inputText[i] != pattern[patternIndex] && pattern[patternIndex + 1] == '?'))
count++;
patternIndex++;
}
return pattern.Length == count;
}
}
**火柴
输入:文本(字符串),查询(字符串)
输出:如果可以在文本中找到查询匹配项,则为true,否则为false
如果没有特殊字符,大多数语言都有一个contains方法来实现这一点。
一个特殊字符:“?”--如果在查询字符串中找到“?”,则表示前一个字符是可选的(匹配0或1次)
示例:
- 没有问号:
- 匹配(“hello World”、“hello”)返回true
- 匹配(“hello World”、“World”)返回false
- 匹配(“hello World”、“o W”)返回true
- 匹配(“hello World”、“W o”)返回false
- 匹配(“hello World”、“h W”)返回false
- 带问号--“l?”表示“可选l”:
- 匹配(“heo-World”、“hel-o”)返回true
- 匹配(“helo World”、“hel?o”)返回true 匹配(“hello World”、“Hell?o”)返回false
- 确保您理解此案例:
- 匹配(“hello World”、“hell?lo”)返回true
- 您可以有多个问号:
- 匹配(“hello World”、“ex?llo Worlds?”)返回true
public class StringPatternMatch
{
public static bool MatchPattern(string inputText, string pattern)
{
int count = 0; int patternIndex = 0;
for (var i = 0; i < inputText.Length; i++)
{
if (patternIndex > pattern.Length)
break;
if (inputText[i] == pattern[patternIndex] ||
(inputText[i] != pattern[patternIndex] && pattern[patternIndex + 1] == '?'))
count++;
patternIndex++;
}
return pattern.Length == count;
}
}
公共类StringPatternMatch
{
公共静态布尔匹配模式(字符串输入文本,字符串模式)
{
整数计数=0;整数模式索引=0;
对于(变量i=0;i模式长度)
打破
if(inputText[i]==模式[patternIndex]||
(输入文本[i]!=模式[patternIndex]&&pattern[patternIndex+1]=='?'))
计数++;
patternIndex++;
}
返回模式。长度==计数;
}
}
从一侧到另一侧遍历两个字符串(例如从最右边的字符到最左边的字符)。如果我们找到一个匹配的字符,我们将在两个字符串中向前移动,增加模式计数器-在模式长度的末尾匹配计数
我也提供了我的代码,但这并不包括所有的情况
当然,下一轮我没有去,但我仍然在思考这个问题,还没有找到准确的解决方案-希望看到一些有趣的答案 您的想法可行,但您的实施过于简化:
// assumes the pattern is valid, e.g. no ??
public static boolean matches(String string, String pattern) {
int p = 0; // position in pattern
// because we only return boolean we can discard all optional characters at the beginning of the pattern
while (p + 1 < pattern.length() && pattern.charAt(p + 1) == '?')
p += 2;
if (p >= pattern.length())
return true;
for (int s = 0; s < string.length(); s++) // s is position in string
// find a valid start position for the first mandatory character in pattern and check if it matches
if (string.charAt(s) == pattern.charAt(p) && matches(string, pattern, s + 1, p + 1))
return true;
return false;
}
private static boolean matches(String string, String pattern, int s, int p) {
if (p >= pattern.length()) // end of pattern reached
return true;
if (s >= string.length() || string.charAt(s) != pattern.charAt(p)) // end of string or no match
// if the next character of the pattern is optional check if the rest matches
return p + 1 < pattern.length() && pattern.charAt(p + 1) == '?' && matches(string, pattern, s, p + 2);
// here we know the characters are matching
if (p + 1 < pattern.length() && pattern.charAt(p + 1) == '?') // if it is an optional character
// check if matching the optional character or skipping it leads to success
return matches(string, pattern, s + 1, p + 2) || matches(string, pattern, s, p + 2);
// the character wasn't optional (but matched as we know from above)
return matches(string, pattern, s + 1, p + 1);
}
//假设模式有效,例如否??
公共静态布尔匹配(字符串、字符串模式){
int p=0;//模式中的位置
//因为我们只返回布尔值,所以可以放弃模式开头的所有可选字符
而(p+1=pattern.length())
返回true;
for(int s=0;s=pattern.length())//到达模式的末尾
返回true;
如果(s>=string.length()| | string.charAt(s)!=pattern.charAt(p))//字符串结束或不匹配
//如果模式的下一个字符是可选的,请检查其余字符是否匹配
返回p+1
您错过的一件大事是模式可以从输入字符串中的任意点开始。尝试修复代码,使其返回示例的正确答案?如果你花时间尝试掌握算法,而不是四处寻找解决方案,你会学到更多。匹配(“hello World”,“o C”)返回true
?@vivek_23我的错,我有一个问题-我已经纠正了!如果您了解DCGs,那么就不值得一提了。您没有给出编程语言,也没有说明列表是否可以视为数组。e、 g“hel”、“l”[])、“o”
是一个或运算符,,
是and运算符。看起来它的时间复杂度将是O(N^2)@vran我认为对于现实生活中的示例,性能相当好,但大O可能更差。例如,lllllll
和lll?a
。您可以先尝试匹配必填字符,然后尝试用它们之间的可选字符填补空白。