当前位置:网站首页>leetcode1221. Split balance string
leetcode1221. Split balance string
2022-06-25 04:41:00 【2021dragon】

LeetCode Series articles
List of articles
One 、 Title Description
In a balanced string , ′ L ′ 'L' ′L′ and ′ R ′ 'R' ′R′ The number of characters is the same .
Give you a balanced string s s s, Please split it into as many balanced strings as possible .
Returns the maximum number of balanced strings that can be split .
Be careful : Each split string must be a balanced string , And the balanced string obtained by segmentation is a continuous substring of the original string .
Two 、 Example
Input : s = “RLRRLLRLRL”
Output : 4
explain : s Can be divided into “RL”、“RRLL”、“RL”、“RL”, Each substring contains the same number of ‘L’ and ‘R’ .
3、 ... and 、 Main idea
This topic actually investigates the greedy algorithm , That is, when solving problems , Always make what seems to be the best choice at the moment . For this topic , When we iterate over the balanced string for segmentation , Once a sub balanced string is found, it should be split immediately .
For example, for balanced strings “RLRRLLRLRL”, We can traverse to “RL” Split it up , You can also traverse to “RLRRLL” Then split it , because “RL” and “RLRRLL” Are balanced strings .
But the question requires the maximum number of balanced strings obtained by segmentation , So we are traversing to “RL” Should be split , At this time, the following “RRLL” Can be split into another balanced string . Follow this method to traverse the given balanced string , Finally, the maximum number of balanced strings that can be obtained after segmentation .
It should be noted that , The given balance string must eventually be divided into several complete sub balance strings , There will be no case that some of the remaining characters cannot form a balanced string . Because when you separate a sub balanced string from the balanced string , Among the remaining strings R and L The number of must be equal , So this string is also a balanced string .
As shown in the figure below :
Four 、 Code implementation
When analyzing the problem, the split string is just for convenience , In fact, there is no need to segment the string when solving the problem .
- We can define one level Variable , The initial value of this variable is set to 0.
- In the process of balancing strings with variables , If the traversed character is R, On the other hand level Since it increases , If the traversed character is L, On the other hand level Since the subtract .( And vice versa )
- except level Setting of initial value , In the process of traversing level The value of has changed to several times 0, It indicates the maximum number of sub balance strings that the balance string can be divided into .
Dynamic diagram demonstration :
The code is as follows :
边栏推荐
- Immutable学习之路----告别传统拷贝
- CTF_ Web:8-bit controllable character getshell
- Code scanning payment flow chart of Alipay payment function developed by PHP
- 我的IC之旅——资深芯片设计验证工程师成长——“胡”说IC工程师完美进阶
- OpenSea PHP开发包
- After the newly assigned variable of the applet is modified, the original variable will also be modified
- 为什么TCP握手刚刚好是3次呢?
- What is data persistence?
- Concat() in JS
- 515. 在每个树行中找最大值 / 剑指 Offer II 095. 最长公共子序列
猜你喜欢
随机推荐
Wechat likes to pay attention to the solution of invalid automatic reply
Classification of gbase 8s locks
ThinkPHP is integrated with esaywechat. What's wrong with wechat payment callback without callback?
《牛客刷verilog》Part I Verilog快速入门
Upgrade PHP to php7 The impact of X (I). The problem of session retention. Keep login
Vscode 设置clang-format
What is the storage engine and the three common database storage engines for MySQL
Unity Quad culls shaders with back faces and transparent parts
大话云原生数据库中的存算分离
SQL注入详解
Retrofit 源码分析
GBASE 8S内存管理
How to screen out words related to products and eliminate invalid words accurately
华为鸿蒙开发第四课
js的sort()函数
GBASE 8s存储过程语法结构
js的call()和apply()
Solution of gbase 8s livelock and deadlock
【Flink】RocksDB增量模式checkpoint大小持续增长的问题及解决
OOP栈类模板(模板+DS)













