Skip to content

Commit 26dbaf6

Browse files
Add 3 in c language
1 parent 68c75ea commit 26dbaf6

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Given a string s, find the length of the longest substring without repeating characters.
3+
Time: O(n)
4+
Space: O(1)
5+
*/
6+
7+
int max(int a, int b) {
8+
return a>b?a:b;
9+
}
10+
11+
int lengthOfLongestSubstring(char * s){
12+
int alpha[128] = {0};
13+
int i=0;
14+
int j=0;
15+
int ans=0;
16+
while (s[j]!='\0') {
17+
alpha[s[j]]++;
18+
if (alpha[s[j]]>1) {
19+
while (alpha[s[j]]>1) {
20+
alpha[s[i]]--;
21+
i++;
22+
}
23+
}
24+
ans = max(ans, j-i+1);
25+
j++;
26+
}
27+
return ans;
28+
}

0 commit comments

Comments
 (0)