fork(3) download
  1. import itertools
  2.  
  3. def count_plus_minusminus(s):
  4. c = 0
  5. best = 0;
  6. for ch, g in itertools.groupby(s):
  7. count = sum(1 for _ in g)
  8. if ch == '-':
  9. c = c + count // 2
  10. if count % 2 == 1:
  11. best = max(best, c)
  12. c = count // 2
  13. elif ch == '+':
  14. c = c + count
  15. return max(best, c)
  16.  
  17.  
  18. for t in ["-+--+", "--+---+-", "++-+--+----+-+", "+--+++-------+-+++-+"]:
  19. print(f"{t} = {count_plus_minusminus(t)}")
  20.  
Success #stdin #stdout 0.02s 9224KB
stdin
Standard input is empty
stdout
-+--+ = 3
--+---+- = 3
++-+--+----+-+ = 6
+--+++-------+-+++-+ = 8