【问题标题】:Find a match for query within text在文本中查找匹配查询
【发布时间】:2019-09-10 05:15:50
【问题描述】:

最近在面试的时候问了这个问题,我无法解决,所以需要一些建议我该如何解决这个问题

声明:我不能使用 REGEX 或任何内置库

*****问题陈述如下*********

**匹配 输入:文本(字符串)、查询(字符串) 输出:如果您可以在文本中找到匹配的查询,则为 true,否则为 false 如果没有特殊字符,大多数语言都有一个 contains 方法可以做到这一点。 一个特殊字符:'?' ——如果你找到“?”在查询字符串中,它表示前一个字符是可选的(匹配 0 或 1 次)。

例子:

  • 没有问号:
  • matches("hello World", "hello") 返回真
    • matches("hello World", "world") 返回 false
    • matches("hello World", "o W") 返回真
    • matches("hello World", "W o") 返回 false
    • matches("hello World", "h W") 返回 false
    • 带问号 -- “l?”表示“可选 l”:
    • matches("heo World", "hel?o") 返回 true
    • matches("helo World", "hel?o") 返回真 match("hello World", "hel?o") 返回 false
    • 确保您了解此案例:
    • matches("hello World", "hell?lo") 返回 true
    • 您可以有多个问号:
    • matches("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;
    }
}

从一侧到另一侧遍历两个字符串(例如从最右边的字符到最左边)。如果我们找到匹配的字符,我们会在两个字符串中继续前进,并增加模式计数器 - 最后匹配计数与模式长度

我也提供了我的代码,但并不涵盖所有情况

当然我没去下一轮,但我还在思考这个问题,还没有找到准确的解决方案-希望看到一些有趣的答案!

【问题讨论】:

  • 您错过的重要一点是模式可以从输入字符串中的任何点开始。尝试修复您的代码,使其返回示例的正确答案?如果您花时间尝试掌握算法而不是四处寻找解决方案,您将学到更多。
  • matches("hello World", "o C") returns true ?
  • @vivek_23 我的错,我的错字有问题 - 我已经更正了!
  • 这可以用DCG 完成,所以如果你知道DCGs,那就不值得一提了。您没有给出编程语言,也没有说列表是否可以视为数组。例如 "hel",("l";[]),"o". ;or 运算符,, 是和“和”运算符。

标签: c# string algorithm data-structures


【解决方案1】:

您的想法可行,但您的实施过于简单:

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

【讨论】:

  • 看起来时间复杂度为 O(N^2)
  • @vran 我认为对于现实生活中的例子来说,性能相当不错,但大 O 可能会更糟。例如。 lllllllll?l?a。您可以先尝试匹配强制字符,然后尝试用它们之间的可选字符填补空白。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-13
  • 2016-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-07
  • 2019-05-06
相关资源
最近更新 更多