当前位置:网站首页>Longest substring without repeated characters (C language)
Longest substring without repeated characters (C language)
2022-06-23 08:49:00 【diqiudq】
subject : Given a string p, Please find out the length of the longest substring without repeating characters .
1 Violent solution
For an arbitrary string , To find its longest substring , It's easy to think of traversing each substring . The following code will traverse each substring , For one of the substrings , We also need to determine whether it satisfies the no repetition character .
Here the string length is len, The number of all non empty substrings is len!( Factorial ) individual , The time complexity of each judgment of whether there are no repeated characters is O(n²), So this algorithm is very slow . When the string length is very long , Basically no results .
#include <stdio.h>
#include <string.h>
int main(){
int len, output=0, i, j, k, m, sub_status=1;
char p[] = "aaabc";
len = strlen(p);
for(i=0;i<len+1;i++){
for(j=0;j<len-i+1;j++){
sub_status=1;
for(k=j;k<j+i-1;k++){
for(m=k+1;m<j+i;m++){
if(*(p+k)==*(p+m)){
sub_status=0;
break;
}
}
if(sub_status==0)continue;
}
if(sub_status==1){
output=i;
break;
}
}
}
printf("%d", output);
}2 Better way
The characters in the substring appear consecutively in the original string , This feature allows us to reduce “ Violence solution ” The overhead of determining whether there are repeated characters in the . We can put the elements in the queue one by one , After putting new elements , We determine whether there are duplicate characters in the original queue . If there is , Then discard this repeating character and all previous characters , Get a new queue , This keeps the queue free of duplicate elements . In the process , Each resulting new queue is a substring with no duplicate characters , We just compare their lengths and choose the longest one .
#include <stdio.h>
#include <string.h>
int main(){
int len, output=0, i, j, head=0, tmp;
char p[] = "jfdksliwi";
len = strlen(p);
for(i=1;i<len;i++){
for(j=head;j<i;j++){
if(*(p+j)==*(p+i)){
head=j+1;
break;
}
}
tmp = i+1-head;
if(tmp>output)
output = tmp;
}
if(len-head>output)
output = len-head;
printf("%d", output);
}边栏推荐
- 【活动报名】SOFAStack × CSDN 联合举办开源系列 Meetup ,6 月 24 日火热开启
- On the light application platform finclip and the mobile application development platform mpaas
- 65. Valid Number
- List interface three sub implementation classes
- TDesign update weekly report (the first week of January 2022)
- 社区文章|MOSN 构建 Subset 优化思路分享
- node request模块cookie使用
- Leetcode topic analysis sort colors
- Driver Architecture & platform platform bus driver model
- Use newbeecoder UI implements data paging
猜你喜欢

Object. Defineproperty() and data broker

Data assets are king, analyzing the relationship between enterprise digital transformation and data asset management

636. Exclusive Time of Functions

Which is better, semrush or ahrefs? Which is more suitable for GoogleSEO keyword analysis

6月《中国数据库行业分析报告》发布!智能风起,列存更生

Linux Mysql安装

Why do we say that the data service API is the standard configuration of the data midrange?

通信方式总结及I2C驱动详解

Flink错误--Caused by: org.apache.calcite.sql.parser.SqlParseException: Encountered “time“

Talk about the implementation principle of @autowired
随机推荐
Leetcode topic analysis sort colors
词性家族
Talk about the implementation principle of @autowired
Lighthouse cloud desktop experience
Detailed explanation of srl16e in xilinxffpga
3. caller service call - dapr
2022.6.22-----leetcode.513
单编内核驱动模块
Multi-scale feature combination in target detection
6-shining laser application of calayer
Android kotlin coroutines KTX extension
Set interface and set sub implementation classes
297. Serialize and Deserialize Binary Tree
点云库pcl从入门到精通 第十章
Testing -- automated testing selenium (about API)
Summary of Arthas vmtool command
社区文章|MOSN 构建 Subset 优化思路分享
【活动报名】SOFAStack × CSDN 联合举办开源系列 Meetup ,6 月 24 日火热开启
670. Maximum Swap
How thingjs enables low threshold 3D visualization development