当前位置:网站首页>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;
}
}
边栏推荐
- LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target
- 2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
- CF1638E colorful operations
- 如何看待智慧城市建设中的改变和机遇?
- js 图片路径转换base64格式
- Codeworks round 449 (Div. 1) C. Kodori tree template
- Why is Internet thinking not suitable for AI products?
- TCP server communication flow
- Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
- Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
猜你喜欢

Pytorch(三) —— 函数优化

数据加载及预处理

Difficulties in the development of knowledge map & the importance of building industry knowledge map

【硬十宝典】——2.【基础知识】开关电源各种拓扑结构的特点

Why is Internet thinking not suitable for AI products?

Basic usage, principle and details of session

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

Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom

Programs and processes, process management, foreground and background processes

2022 hoisting machinery command registration examination and hoisting machinery command examination registration
随机推荐
Ten wastes of software research and development: the other side of research and development efficiency
【硬十宝典】——1.【基础知识】电源的分类
How to view the changes and opportunities in the construction of smart cities?
Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
About the transmission pipeline of stage in spark
一些小知识点
细数软件研发效能的七宗罪
I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
[difficult] sqlserver2008r2, can you recover only some files when recovering the database?
Simple implementation of slf4j
网站服务器:好用的网站服务器怎么选这五方面要关注
CF1638E colorful operations
Threejs opening
2022年上海市安全员C证考试题模拟考试题库及答案
OSPF notes [dr and bdr]
What is uid? What is auth? What is a verifier?
2022年聚合工艺考试题及模拟考试
Haskell lightweight threads overhead and use on multicores
MySQL winter vacation self-study 2022 12 (5)
Common UNIX Operation and maintenance commands of shell