leetcode chicken dish wits series (6) --- check a string field, there are several symmetrical

1. Original title:

https://leetcode.com/problems/split-a-string-in-balanced-strings/

Split a String in Balanced Strings:

Balanced strings are those who have equal quantity of 'L' and 'R' characters.

Given a balanced string s split it in the maximum amount of balanced strings.

Return the maximum amount of splitted balanced strings.

translation:

Symmetric fields meaning: LLRR or LR this, now given a string, then find there are several symmetrical field.

 

2. Problem-solving ideas:

This question is actually very straightforward, you just use the for loop to control the number on the line. Because there is a string of several symmetrical fields, so you only need to record the same number every time the L and R on the line, because this time is certainly a new symmetry.

 

This question I was lazy to use the python2, in fact, a grammar of any language will do:

 

Solution class (Object):
DEF balancedStringSplit (Self, S):
count_tmp1 = count_tmp2 = count_tmp3 = 0 
for S in S: # retrieve the entire string
IF (S == "R & lt"):
count_tmp1. 1 #R + = number
else :
count_tmp2 + = number #L. 1
if (count_tmp1 == count_tmp2): # when the two are consistent, indicating a new symmetric
count_tmp3 + =. 1
return count_tmp3

Guess you like

Origin www.cnblogs.com/cptCarlvon/p/12124437.html