当前位置:网站首页>Find the longest substring of a string
Find the longest substring of a string
2022-04-21 16:24:00 【Group field】
Given a string s, find the length of the longest substring without repeating characters.
Example 1:
Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, “pwke” is a subsequence and not a substring.
Constraints:
0 <= s.length <= 5 * 104
s consists of English letters, digits, symbols and spaces.
class Solution {
public:
string longestSubstring(string s) {
unsigned long n = s.length();
int start = 0;
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (checkRepetition(s, i, j)) {
if(res<j-i+1){
res = j-i+1;
start = i;
}
}
}
}
return s.substr(start, res);
}
int lengthOfLongestSubstring(string s) {
unsigned long n = s.length();
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (checkRepetition(s, i, j)) {
res = max(res, j - i + 1);
}
}
}
return res;
}
bool checkRepetition(string& s, int start, int end) {
vector<int> chars(128);
for (int i = start; i <= end; i++) {
char c = s[i];
chars[c]++;
if (chars[c] > 1) {
return false;
}
}
return true;
}
};
int main(int argc, const char * argv[])
{
Solution s;
string str = "123aabasdfasdfsadfasl";
cout<<"longest Substring is:"<<s.longestSubstring(str)<<endl;
cout<<s.lengthOfLongestSubstring(str)<<endl;
}
版权声明
本文为[Group field]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211602234806.html
边栏推荐
- 在线词典网站
- What is the anti correlation principle? How to choose the anti Association fingerprint browser? What are the criteria?
- 嵌入式GUI盘点-你了解几款?
- Buuctf's [actf2020 freshman competition] BackupFile
- Project training 2022-4-21 (flame grass)
- 哪种耳机戴着耳朵不痛?不入耳佩戴的骨传导耳机
- OJ每日一练——等级成绩
- Historical evolution, application and security requirements of the Internet of things
- . net swagger configuration
- The console displays VM + number + file name to debug
猜你喜欢

物联网的历史演进、应用和安全要求

Solution to the problem of file damage caused by forced shutdown and power failure during nodejs FS readfilesync

SIGIR 2022 | reinforcement learning recommendation system from the perspective of prompt

Haotian Xuhui signed a contract with Changyang technology to jointly build a new ecosystem of industrial Internet security

新媒体人必备的10个效率工具,神器收藏起来

2022 bone conduction earphone ranking, the best bone conduction earphone recommended

云呐:大型医疗设备资产管理系统贵吗?医院资产管理的主要内容

dom操作元素 总结

2022骨传导耳机排名,最好用的骨传导耳机推荐

哪种耳机戴着耳朵不痛?不入耳佩戴的骨传导耳机
随机推荐
2022 bone conduction earphone ranking, the best bone conduction earphone recommended
Assembly language programming: design and debugging of input character type statistics in modular programming
mysql查询某一个字段是否包含中文汉字
Inconsistent length of input and select in elmentui form
MySQL queries whether a field contains Chinese characters
Transformer model technology long article
【2023校招刷题】华为性格测评(综合测评)战略指南
RAM运行内存是什么
SQL--数据库的操作(DDL,DML,DQL)+使用命令查看当前数据库的存储位置(数据库版本查询)
手机里的NPU可以起到什么作用
C# 滑动验证码|拼图验证|SlideCaptcha
Use of Arthas tunnel
手把手教你基于LXD用OAK-D和ROS noetic做ORB SLAM3
必刷|2022年重庆最新八大员之(安全员)模拟题库及答案
Data processing input box section
elmentUI表单中input 和select长度不一致问题
dom操作元素 总结
Changan dark blue's first product can be pure electricity, extended range and hydrogen electricity, with an acceleration of 5.9s
LCD和OLED屏幕有哪些异同
App和小程序有哪些区别?