当前位置:网站首页>LeetCode Daily Question (1573. Number of Ways to Split a String)
LeetCode Daily Question (1573. Number of Ways to Split a String)
2022-08-10 22:02:00 【wangjun861205】
Given a binary string s, you can split s into 3 non-empty strings s1, s2, and s3 where s1 + s2 + s3 = s.
Return the number of ways s can be split such that the number of ones is the same in s1, s2, and s3. Since the answer may be too large, return it modulo 109 + 7.
Example 1:
Input: s = “10101”
Output: 4
Explanation: There are four ways to split s in 3 parts where each part contain the same number of letters ‘1’.
“1|010|1”
“1|01|01”
“10|10|1”
“10|1|01”
Example 2:
Input: s = “1001”
Output: 0
Example 3:
Input: s = “0000”
Output: 3
Explanation: There are three ways to split s in 3 parts.
“0|0|00”
“0|00|0”
“00|0|0”
Constraints:
- 3 <= s.length <= 105
- s[i] is either ‘0’ or ‘1’.
整体分成 3 种情况:
- 全是 0 的情况
- 1 The number cannot be used 3 整除的情况
- 1 The number can be used 3 整除的情况
情况 2 很简单,直接返回 0 即可.
情况 1 We are actually equivalent to putting 2 partitions to any 2 个槽里, We think of a slot between two numbers, 比如"0000"就有 3 个槽, When we put the first baffle in slot[i]里面的时候, Our second baffle can be placed onThe total number of slots-i-1
in a slot, 注意 i 从 0 开始, So we just need to add up the number of options, 时间复杂度 O(n)
情况 2 In fact, what we are looking for is between the three groups 0 的数量, 比如"10100101000101", 我们可以把它拆解为 101|00|101|000|101, 每组含有 1 Both ends of the group must be 1, That is, the smallest group that meets the requirements of the question, 这其中的 00 和 000, 与两边的 1 The combination is made respectively 1001, 10001, We can place baffles on any of these slots, The separated groups must meet the subject criteria, 1001 有 3 个槽, 10001 有 4 个槽, So our actual choice has 3 * 4 = 12 种
impl Solution {
pub fn num_ways(s: String) -> i32 {
let one_num = s.chars().filter(|v| v == &'1').count();
if one_num % 3 != 0 {
return 0;
}
let mut ans = 0i64;
if one_num == 0 {
let slot_num = s.len() - 1;
for i in 0..slot_num {
ans += slot_num as i64 - i as i64 - 1;
}
return (ans % (10i64.pow(9) + 7)) as i32;
}
let mut zero_span1 = 0;
let mut zero_span2 = 0;
let mut prev_one_index = -1;
let mut ones = 0;
for (i, c) in s.chars().enumerate() {
if c == '1' {
ones += 1;
if ones == one_num / 3 + 1 {
if prev_one_index == -1 {
zero_span1 = i as i64 + 1;
} else {
zero_span1 = i as i64 - prev_one_index;
}
} else if ones == one_num / 3 * 2 + 1 {
if prev_one_index == -1 {
zero_span2 = i as i64 + 1;
} else {
zero_span2 = i as i64 - prev_one_index;
}
break;
}
prev_one_index = i as i64;
}
}
(zero_span1 * zero_span2 % (10i64.pow(9) + 7)) as i32
}
}
边栏推荐
- 管理员必须知道的RADIUS认证服务器的部署成本
- shell脚本循环语句for、while语句
- 【PCBA scheme design】Bluetooth skipping scheme
- How to translate financial annual report, why choose a professional translation company?
- 异常的了解
- 黑猫带你学Makefile第13篇:Makefile编译问题合集
- 阿里巴巴、蚂蚁集团推出分布式数据库 OceanBase 4.0,单机部署性能超 MySQL
- SELECT:简单的查询操作语法&使用例——《mysql 从入门到内卷再到入土》
- What are the concepts, purposes, processes, and testing methods of interface testing?
- 微擎盲盒交友变现-vp_ph打开慢优化
猜你喜欢
C # Hex file transfer skills necessary article 】 【 bin file code implementation
The use of TortoiseSVN little turtle
深度学习之 12 循环神经网络RNN2
Conditional Statements of Shell Programming (2)
Using SylixOS virtual serial port, serial port free implementation system
快消品行业经销商协同系统:实现经销商可视化管理,提高沟通执行效率
LeetCode-498 - Diagonal Traversal
C#【必备技能篇】Hex文件转bin文件的代码实现
c语言之 练习题1 大贤者福尔:魔法数,神奇的等式
【Maui正式版】创建可跨平台的Maui程序,以及有关依赖注入、MVVM双向绑定的实现和演示
随机推荐
RADIUS Authentication Server Deployment Costs That Administrators Must Know
LeetCode-498-对角线遍历
从斐波那契 - 谈及动态规划 - 优化
ACM模板笔记:八数码问题——使用BFS+康托展开打表解决
c语言之 练习题1 大贤者福尔:魔法数,神奇的等式
LeetCode-36-二叉搜索树与双向链表
LeetCode-402-移掉K位数字
Live Classroom System 08-Tencent Cloud Object Storage and Course Classification Management
【PCBA方案设计】蓝牙跳绳方案
数字化转型:如何引导创新领导者
shell(文本打印工具awk)
查询:复杂查询的语法和使用例——《mysql 从入门到内卷再到入土》
PROCEDURE :存储过程结构——《mysql 从入门到内卷再到入土》
Alibaba and Ant Group launched OceanBase 4.0, a distributed database, with single-machine deployment performance exceeding MySQL
D. Game With Array
字节跳动原来这么容易就能进去...
[SQL brush questions] Day3----Special exercises for common functions that SQL must know
Redis Command Manual
元宇宙社交应用,靠什么吸引用户「为爱发电」?
服务——DHCP原理与配置