-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp3.cpp
More file actions
50 lines (38 loc) · 1.17 KB
/
p3.cpp
File metadata and controls
50 lines (38 loc) · 1.17 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Medium
* https://leetcode.com/problems/longest-substring-without-repeating-characters
*/
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
std::string longest_s("");
std::string s_iter("");
std::string curr("");
for (char& c : s) {
curr += c;
if (curr.substr(0, curr.length()-1).find(c) != std::string::npos)
curr = curr.substr(curr.find(c)+1);
if (curr.length() > longest_s.length())
longest_s = curr;
}
return longest_s.length();
}
};
int main(void) {
Solution sol = Solution();
std::string input1 = "abcabcbb";
std::string input2 = "bbbbb";
std::string input3 = "pwwkew";
cout << "input1: " << input1 << endl;
cout << "output: " << sol.lengthOfLongestSubstring(input1) << endl;
cout << "input2: " << input2 << endl;
cout << "output: " << sol.lengthOfLongestSubstring(input2) << endl;
cout << "input3: " << input3 << endl;
cout << "output: " << sol.lengthOfLongestSubstring(input3) << endl;
return 0;
}