当前位置:网站首页>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;
}
}
边栏推荐
- 做网站数据采集,怎么选择合适的服务器呢?
- How to choose the right server for website data collection?
- 扩展-Fragment
- Task04 | statistiques mathématiques
- 软件研发的十大浪费:研发效能的另一面
- Annual inventory review of Alibaba cloud's observable practices in 2021
- Haskell lightweight threads overhead and use on multicores
- 2022年T电梯修理题库及模拟考试
- Talk about testdeploy
- 1. Mobile terminal touch screen event
猜你喜欢

Maixll dock quick start

Maixll-Dock 快速上手

2022 t elevator repair question bank and simulation test

Shell之一键自动部署Redis任意版本

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

TASK04|數理統計

Programs and processes, process management, foreground and background processes

2022年煤气考试题库及在线模拟考试

Why is Internet thinking not suitable for AI products?

OdeInt與GPU
随机推荐
2022危险化学品生产单位安全生产管理人员题库及答案
Seven crimes of counting software R & D Efficiency
Knowledge supplement: redis' basic data types and corresponding commands
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.2 多属性注册协议(Multiple Registration Protocol (MRP))
PgSQL failed to start after installation
Maixll dock quick start
OSPF notes [dr and bdr]
Daily question - line 10
2022年煤气考试题库及在线模拟考试
Account sharing technology enables the farmers' market and reshapes the efficiency of transaction management services
扩展-Fragment
One click shell to automatically deploy any version of redis
VR线上展览所具备应用及特色
[godot] unity's animator is different from Godot's animplayer
Possible problems and solutions of using scroll view to implement slider view
How to do the performance pressure test of "Health Code"
2022 gas examination question bank and online simulation examination
js 图片路径转换base64格式
Learn Chapter 20 of vue3 (keep alive cache component)
离线安装wireshark2.6.10