【问题标题】:Check whether String2 is substring of string1 with '*' and '\'用 '*' 和 '\' 检查 String2 是否是 string1 的子字符串
【发布时间】:2021-08-22 04:15:00
【问题描述】:

给定两个字符串 s1 和 s2,我们必须检查 s2 是否是 s1 的子字符串。 如果是真的打印“真”,否则打印“假” 但是 s1 和 s2 可以包含像 '*' 和 '\' 这样的字符。 在 s2 中,“*”表示两个字母之间的零个或多个字符,“\”表示“*”的转义序列。 在 s1 中,'*' 和 '\' 只是要检查的其他字符。

示例输入和输出:

Input: abcd , a*c   Output: true
Input : spoon , sp*n  Output : true
Input : regex , re*g    Output : true
Input : search , *c Output : true
Input : zoho , *o*o Output : true
Input : zoho , *ogo Output : false
Input : test , pest Output : false
Input : st*r , t\*r Output : true
Input : star , t\*r Output false
Input : tree , tr\  Output false
Input : tr\e , tr\  Output true

我知道这个问题可以通过使用 regex 轻松解决,但是我需要一个合乎逻辑的方法来解决这个问题。有人能尽快帮助我吗? 以下是我尝试过但无法完全解决的代码。

public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.next();
        String s2 = sc.next();
        boolean flag = true;
        int i=0,j=0;
        for(;s2.charAt(j)<'a' || s2.charAt(j)>'z';j++);
        
        for(i=0;i<s1.length();i++)
        {
            char ch = s1.charAt(i);
            
            if(ch==s2.charAt(j))
            {
                i++;j++;
                break;
                
            }
        }

            for(;j<s2.length();)
            {
                
                System.out.println(i+" "+j);
                while(s2.charAt(j)=='*' && j<s2.length()-1 && s2.charAt(j+1)!=s1.charAt(i) && i<s1.length())
                {
                    i++;
                }
                j++;
                if(i==s1.length())
                {
                    flag = false;
                    break;
                }
                
                if(s1.charAt(i)!=s2.charAt(j) && s2.charAt(j)<97 && s2.charAt(j)>122)
                {
                    flag = false;
                    break;
                }
            }
        System.out.print(flag);
        
    }

【问题讨论】:

  • 做正则表达式不是逻辑的事情吗?
  • 这是面试中提出的问题。他们不需要我通过使用正则表达式来解决这个问题。否则,我同意,正则表达式也是合乎逻辑的。

标签: java regex string substring


【解决方案1】:

您可以在遇到 * 字符时剥离 * 字符串并打破它,并检查子字符串是否在实际字符串中。比如:

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String s1 = sc.next();
    String s2 = sc.next();

    int begIndex = 0;
    int prevIndex = 0;
    boolean flag = true;

    s2 = stripLeadingAndTrailingCharacter(s2, '*');

    for(int i = 0; i < s2.length(); ++i) {
      if ((( i == 0 || s2.charAt(i - 1) != '\\') && s2.charAt(i) == '*')
          || (i == s2.length() - 1))  {
        int endIndex = i == s2.length() - 1 ? i + 1 : i;
        String str = s2.substring(begIndex, endIndex).replace("\\*", "*");
        if (s1.contains(str) && prevIndex <= s1.indexOf(str)) {
          prevIndex = s1.indexOf(str);
        } else {
          flag = false;
          break;
        }
        begIndex = i + 1;
      }
    }
    System.out.println(flag);
  }

  public static String stripLeadingAndTrailingCharacter(String s, char ch) {
    int index;
    for (index = 0; index < s.length(); index++) {
      if (s.charAt(index) != ch) {
        break;
      }
    }
    s = s.substring(index);
    for (index = s.length() - 1; index >= 0; index--) {
      if (s.charAt(index) != ch) {
        break;
      }
    }
    s = s.substring(0, index + 1);
    return s;
  }    
      

【讨论】:

  • 非常感谢您的回答!这就是我一直在寻找的。那么,去掉开头和结尾的'*'是解决这个问题的关键吗?
  • s1 = "编辑" s2 = "it*ed" ;在这个测试用例中,答案应该是假的,但它打印的是真。不应更改子字符串的顺序。你能更新你的答案吗?
  • 更新为关注索引,立即查看
  • 非常感谢!标记为已接受的答案。
【解决方案2】:

我不确定 * 和 / 是什么意思,但是,知道一个字符串是否是另一个字符串的子字符串的一种解决方案是使用从 String 类扩展的“contains”方法:

public static void main(String[] args) {
    String str1 = "abcdefghi";
    String str2 = "abcd";
    
    if (str1.contains(str2)) {
        System.out.println("Exist the substring.");
    } else {
        System.out.println("Not exist.");
    }
}

我认为您还需要将如果存在 * 或 / 替换为有效值

【讨论】:

  • 这不能回答我的问题。但无论如何,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
相关资源
最近更新 更多