当前位置:网站首页>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 :
边栏推荐
猜你喜欢

CTF_ Web: Changan cup-2021 old but a little new & asuka

【FLink】access closed classloader classloader.check-leaked-classloader

深度学习——几种学习类型

微信小程序父子组件之间传值

Gbase 8s overall architecture

大话云原生数据库中的存算分离

哪个编程语言实现hello world最烦琐?

Upgrade PHP to php7 The impact of X (I). The problem of session retention. Keep login

Gbase 8s index R tree

Web3 DApp用户体验最佳实践
随机推荐
JS arguments
我的IC之旅——资深芯片设计验证工程师成长——“胡”说IC工程师完美进阶
Introduction to the isolation level of gbase 8s
Musk released humanoid robot. Why is AI significant to musk?
重磅直播 | 相移法+多频外差之数学原理推导+实现
三角形类(构造与析构)
Classification of gbase 8s locks
GBASE 8s的数据视图
Value transfer between parent and child components of wechat applet
Cnpm: unable to load file c:\users\administrator\appdata\roaming\npm\cnpm PS1 because running scripts is prohibited on this system.
Deep learning - several types of learning
LabVIEW开发气体调节器
js的call()和apply()
Office macro virus bounce shell experiment
GBASE 8s的触发器
What is the storage engine and the three common database storage engines for MySQL
How to screen out words related to products and eliminate invalid words accurately
GBASE 8s中DELIMIDENT环境变量的使用
GBASE 8s活锁、死锁问题的解决
Record the problem of C # print size once



