-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnique.cpp
More file actions
31 lines (28 loc) · 721 Bytes
/
Unique.cpp
File metadata and controls
31 lines (28 loc) · 721 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
using namespace std;
bool isUnique(string s, int start, int end) {
bool chars[256] = {false};
for (int i = start; i <= end; i++) {
if (chars[s[i]]) return false;
chars[s[i]] = true;
}
return true;
}
int longestSubstring(string s) {
int n = s.length(), maxLen = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (isUnique(s, i, j)) {
maxLen = max(maxLen, j - i + 1);
}
}
}
return maxLen;
}
int main() {
string s = "abcabcbb";
cout << "Length of longest substring without repeating characters: "
<< longestSubstring(s) << endl;
return 0;
}