【问题标题】:check if string formed using stated set of rules or not检查字符串是否使用规定的规则集形成
【发布时间】:2017-05-13 00:36:52
【问题描述】:

检查字符串是否使用规定的规则集形成。使用以下规则生成:

a.字符串以'a'开头

b.每个 'a' 后面都没有或后面跟着一个 'a' 或 "bb"

c.每个“bb”后面都没有或后面跟着一个“a”

我尝试了以下代码:

public static void main(String[] args) {
    
    Scanner scn = new Scanner(System.in);
    String str = scn.nextLine();
    boolean b = false;
    if (str.charAt(0) == 'a') {
        if (str.charAt(1) == 'b') {
            if (str.charAt(2) == 'b') {
                b = true;
            } else
                b = false;

        } else
            b = false;
    } else
        b = false;
    System.out.println(b);
}

代码没问题吗...??? 对于 input = aab,输出应为 false,对于 input =abba,输出应为 true。

【问题讨论】:

  • 这个问题属于这里:codereview.stackexchange.com
  • 您尝试运行代码了吗?你的结果是什么?
  • 对于上述两个测试用例,它工作正常,但是当我在在线编码网站上尝试此代码时,它正在下降某些测试用例,我不知道为什么。
  • 那个在线评委是什么?此外,如果字符串只是“a”,您的代码将给出空指针
  • 您需要遍历整个字符串,而不仅仅是前 3 个字符。前“abbaaaabba”。

标签: java string


【解决方案1】:

如果允许使用正则表达式,则模式 (a+(bb)?)+ 匹配符合规则的字符串(并且不匹配不符合规则的字符串)。

否则,如果没有某种循环,您的方法可能无法工作,因为字符串 aaaaaaaaaaa 确实与模式匹配。

考虑下面的方法,它应该处理它。

private static boolean stringMatches(String s) {
  // Handle empty and null cases first.
  if (s == null || s.isEmpty()) return false;

  // So long as the string continues to match the pattern, keep stripping
  // characters from it until it is empty. If you reach empty, it matches the pattern.
  while (! s.isEmpty()) {
    // If the first character isn't 'a', we don't match; return false.
    if (s.charAt(0) != 'a') {
      return false;
    }

    // Check for abb, if so strip all of that, otherwise strip just the a
    if (s.length() >= 3 && "abb".equals(s.substring(0,3)) {
      s = s.substring(3);
    } else {
      s = s.substring(1);
    }
  }
  // Reached empty string, return true.
  return true;
}

【讨论】:

  • 是的,你是对的,我应该使用循环,我没有想到,因为我只是看到了这 2 个测试用例并尝试为其编写代码。
【解决方案2】:

这是递归方法:

public static boolean checkAB(String s) 
{
    if (s.length()==0) 
        return true;
    if (s.charAt(0) != 'a')
        return false;
    if (s.length() >= 3 && "abb".equals(s.substring(0,3)))
        return checkAB(s.substring(3));
    else
        return checkAB(s.substring(1));
}

【讨论】:

    【解决方案3】:

    这是我的sn-p:

    def checkAB(str):
        if len(str) == 0:
            return True
        if len(str) == 1:
            if str == 'a':
                return True
            else:
                return False
        if str[0] == 'a':
            return checkAB(str[1:])
        elif str[0] == 'b':
            if str[1] == 'b':
                return checkAB(str[2:])
            else:
                return False
        else:
            return False
    

    尝试读取布尔值并以字符串“true”或“false”打印输出。就我而言,我在这里直接返回布尔值犯了一个错误。

    再来一个sn-p:

    def checkAB(str):
        if (len(str) == 0):
            return True
    
        if (str[0] == 'a'):
            if (len(str[1:]) > 1 and str[1:3] == 'bb'):
                return checkAB(str[3:])
            else:
                return checkAB(str[1:])
        else:
            return False
    

    希望对你有帮助。

    【讨论】:

    • 这如何确保'a' is followed by nothing or an 'a' each "bb" is followed by nothing or an 'a'?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 2013-11-27
    • 1970-01-01
    • 2019-07-24
    • 2020-09-09
    • 2015-09-10
    相关资源
    最近更新 更多