当前位置:网站首页>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 :
边栏推荐
- i. Max development board learning record
- CTF_ Web: basic 12 questions WP of attack and defense world novice zone
- Record small knowledge points
- GBASE 8s存儲過程語法結構
- mongodb集群
- Machine learning deep learning -- Vectorization
- Anaconda installation +tensorflow installation +keras installation +numpy installation (including image and version information compatibility issues)
- [Flink] problems and solutions of the continuous growth of checkpoint size in rocksdb incremental mode
- Bingbing's learning notes: implementation of circular queue
- The solution of wechat applet switchtab unable to take parameters
猜你喜欢

重磅直播 | 相移法+多频外差之数学原理推导+实现

机器学习深度学习——向量化

Mongodb cluster

Record small knowledge points

Record the problem of C # print size once

CTF_ Web:php weak type bypass and MD5 collision

高效的NoSQL数据库服务Amozon DynamoDB体验分享

Simple text analysis of malicious samples - Introduction

2.0springmvc uses restful

Anaconda安装+TensorFlow安装+Keras安装+numpy安装(包含镜像和版本信息兼容问题)
随机推荐
领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
GBASE 8s 索引R树
OOP 向量加减(友元+拷贝构造)
哪个编程语言实现hello world最烦琐?
CTF_ Variable coverage in web:php
2.0SpingMVC使用RESTful
A detailed summary of TCP connection triple handshake
Coordinate system left multiply right multiply
STM32的DMA双缓冲模式详解
写shell脚本报错总结
Data view for gbase 8s
Gbase 8s stored procedure syntax structure
After the newly assigned variable of the applet is modified, the original variable will also be modified
PostgreSQL database Wal - RM_ HEAP_ ID logging action
Office macro virus bounce shell experiment
Immutable学习之路----告别传统拷贝
Retrofit source code analysis
Code scanning payment flow chart of Alipay payment function developed by PHP
Record the problem of C # print size once
Anaconda installation +tensorflow installation +keras installation +numpy installation (including image and version information compatibility issues)



