当前位置:网站首页>Leecode records the number of good segmentation of 1525 strings
Leecode records the number of good segmentation of 1525 strings
2022-07-01 04:41:00 【Why is there a bug list】
topic
Give you a string s , A partition is called 「 Good segmentation 」 When it satisfies : take s Divided into 2 A string p and q , They connect together to be equal to s And p and q The number of different characters in is the same .
Please return s The number of good divisions .
Example 1:
Input :s = “aacaba”
Output :2
explain : All in all 5 Sort of split string “aacaba” Methods , among 2 The kind is good segmentation .
(“a”, “acaba”) The left string and the right string contain 1 And 3 Different characters .
(“aa”, “caba”) The left string and the right string contain 1 And 3 Different characters .
(“aac”, “aba”) The left string and the right string contain 2 And 2 Different characters . It's a good segmentation .
(“aaca”, “ba”) The left string and the right string contain 2 And 2 Different characters . It's a good segmentation .
(“aacab”, “a”) The left string and the right string contain 3 And 1 Different characters .
Example 2:
Input :s = “abcd”
Output :1
explain : Good segmentation is to divide the string into (“ab”, “cd”) .
Example 3:
Input :s = “aaaaa”
Output :4
explain : All segmentation is good segmentation .
Example 4:
Input :s = “acbadbaada”
Output :2
Tips :
s Only lowercase letters .
1 <= s.length <= 10^5
answer
class Solution {
public int numSplits(String s) {
int n = s.length();
int[] left = new int[n + 2];
int[] right = new int[n + 2];
boolean[] recLeft = new boolean[26];
boolean[] recRight = new boolean[26];
for (int i = 1; i <= n; i++) {
int c = s.charAt(i - 1) - 'a';
if (recLeft[c]) {
left[i] = left[i - 1];
} else {
recLeft[c] = true;;
left[i] = left[i - 1] + 1;
}
}
for (int i = n; i > 0; i--) {
int c = s.charAt(i - 1) - 'a';
if (recRight[c]) {
right[i] = right[i + 1];
} else {
recRight[c] = true;
right[i] = right[i + 1] + 1;
}
}
int ret = 0;
for (int i = 1; i < n; i++) {
if (left[i] == right[i + 1]) {
ret++;
}
}
return ret;
}
}
边栏推荐
- Openresty rewrites the location of 302
- [ue4] event distribution mechanism of reflective event distributor and active call event mechanism
- 2022年煤气考试题库及在线模拟考试
- LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target
- Difficulties in the development of knowledge map & the importance of building industry knowledge map
- VIM简易使用教程
- 2022危险化学品生产单位安全生产管理人员题库及答案
- After many job hopping, the monthly salary is equal to the annual salary of old colleagues
- 先有网络模型的使用及修改
- Question bank and answers for chemical automation control instrument operation certificate examination in 2022
猜你喜欢

2022 hoisting machinery command registration examination and hoisting machinery command examination registration

Internet winter, how to spend three months to make a comeback

2022年聚合工艺考试题及模拟考试

Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor

Execution failed for task ‘:app:processDebugResources‘. > A failure occurred while executing com. and

Use winmtr software to simply analyze, track and detect network routing

Mallbook: how can hotel enterprises break the situation in the post epidemic era?

2022年G1工业锅炉司炉特种作业证考试题库及在线模拟考试

2022 t elevator repair question bank and simulation test

数据加载及预处理
随机推荐
What are permissions? What are roles? What are users?
Some small knowledge points
The design points of voice dialogue system and the importance of multi round dialogue
This sideline workload is small, 10-15k, free unlimited massage
【LeetCode】100. Same tree
How to use maixll dock
先有网络模型的使用及修改
OSPF notes [multiple access, two multicast addresses with OSPF]
2. Use of classlist (element class name)
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.2 多属性注册协议(Multiple Registration Protocol (MRP))
[leetcode skimming] February summary (updating)
Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling
2022年上海市安全员C证考试题模拟考试题库及答案
selenium打开chrome浏览器时弹出设置页面:Mircrosoft Defender 防病毒要重置您的设置
I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
[human version] Web3 privacy game in the dark forest