二进制字符串(01010 .....)问题

San*_*pal 0 binary

给定一串0和1.如何找到0和1的数量相等的子串的最大长度.

Ski*_*izz 5

看到您正在寻找符合条件的最长字符串,合理的算法将是:

start with whole string
does string match criteria?
yes: we're done
no:
  (1) find all sub strings of length (whole string - 1)
  do any of these match the criteria?
  yes: we're done
  no:
    repeat from (1) but use (whole string - 2), then (whole string - 3), etc until sub string length is 2
Run Code Online (Sandbox Code Playgroud)

请注意,奇数长度的字符串永远不能匹配所需的条件,因此您可以减少程序的工作量.

你的下一个问题无疑将是'我如何在C中做到这一点',这是你需要先做一些工作的地方(假设这是作业,看起来确实如此).你不会从这里得到'teh codez'.但是,如果您发布一些无法按预期工作的代码,您将获得许多有用的建议,以指导您找到正确的解决方案.