1221. 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.

Example 1:

Input: s = "RLRRLLRLRL"
Output: 4
Explanation: s can be split into "RL", "RRLL", "RL", "RL", each substring contains same number of ‘L‘ and ‘R‘.
Example 2:

Input: s = "RLLLLRRRLR"
Output: 3
Explanation: s can be split into "RL", "LLLRRR", "LR", each substring contains same number of ‘L‘ and ‘R‘.
Example 3:

Input: s = "LLLLRRRR"
Output: 1
Explanation: s can be split into "LLLLRRRR".

Constraints:

1 <= s.length <= 1000
s[i] = ‘L‘ or ‘R‘

python3:

 1 class Solution:
 2     def balancedStringSplit(self, s: str) -> int:
 3         l = 0
 4         r = 0
 5         rst = 0
 6         for s_str in s:
 7             if s_str == ‘L‘:
 8                 l += 1
 9             elif s_str == ‘R‘:
10                 r += 1
11             if l == r:
12                 rst += 1
13                 l = 0
14                 r = 0
15         return rst

原文地址:https://www.cnblogs.com/dean757/p/11686125.html

时间: 2024-10-18 09:07:08

1221. Split a String in Balanced Strings的相关文章

C# String.split()用法小结。String.Split 方法 (String[],?StringSplitOptions)

split()首先是一个分隔符,它会把字符串按照split(' 字符')里的字符把字符串分割成数组,然后存给一个数组对象. 输出数组对象经常使用foreach或者for循环. 第一种方法 string s=abcdeabcdeabcde; string[] sArray=s.Split('c') ; foreach(string i in sArray) Console.WriteLine(i.ToString()); 输出下面的结果: ab deab deab de 第二种方法 我们看到了结果

string数据类型与strings模块 | Go语言基础

package main // 当前包声明 func main() { // 程序的入口 // string // Go中的字符串是一个字节的切片,是Unicode兼容的,并且是UTF-8编码的. // 字符串是一些字节的集合,可以理解为一个字符的序列. // 每个字符都又固定的位置 // 定义字符串 tempString:="hello" tempString := `hello` // 根据下表获取字符串 fmt.Println(tempString[0]) // 104 fmt.

String(205.Isomorphic Strings)

Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters.

如何分割(split)string字符串

使用String#split()方法 如下所示: String string = "004-034556"; String[] parts = string.split("-"); String part1 = parts[0]; // 004 String part2 = parts[1]; // 034556 需要注意的是,该方法的参数是个正则表达式,要注意对某些字符做转码.例如,.在正则表达式中表示任意字符,因此,如果你要通过.号做分割,需要这样写,split

split a string in C++

This is my favourite way to iterate through a string. You can do what you want per word. string line = "a line of text to iterate through"; string word; istringstream iss(line, istringstream::in); while( iss >> word ) { ... }

CsQuery获取IDomObject元素的完整CSS选择器

一.方法说明 通过IDomObject元素,获取完整的CSS选择器,过滤HTML和BODY元素,自动将class.id添加到选择器上,优先添加class,无class再添加id.如: <html> <head></head> <body> <div id="result"> <h3 class="t"><a "="" href="http://www

java基础----&gt;String中的split方法的原理

这里面主要介绍一下关于String类中的split方法的使用以及原理. split函数的说明 split函数java docs的说明: When there is a positive-width match at the beginning of this string then an empty leading substring is included at the beginning of the resulting array.A zero-width match at the beg

split 实现(c++ string)

#include <iostream> #include <vector> size_t split(std::string &src, std::vector<std::string> *tokens, std::string sep) { size_t last= 0; size_t index = src.find(sep, last); size_t length = src.size(); while(index != std::string::npo

java的string.split()分割特殊字符时注意点

[1]单个符号作为分隔符 String address="上海|上海市|闵行区|吴中路"; String[] splitAddress=address.split("\\|"); //如果以竖线为分隔符,则split的时候需要加上两个斜杠[\\]进行转义 System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]); String address="上