-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.cpp
75 lines (60 loc) · 1.49 KB
/
3.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <gtest/gtest.h>
#include <string>
#include <unordered_map>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s)
{
unordered_map<char, int> hash;
int max_length = 0;
int start = 0;
for (int i = 0; i < s.length(); i++) {
char c = s[i];
if (hash.find(c) != hash.end() && start <= hash[c]) {
start = hash[c] + 1;
}
else {
max_length = max(max_length, i - start + 1);
}
hash[c] = i;
}
return max_length;
}
};
class Testing : public testing::Test {
public:
Solution solution;
void SetUp() {}
void TearDown() {}
};
TEST_F(Testing, Case1)
{
EXPECT_EQ(solution.lengthOfLongestSubstring("abcabcbb"), 3);
}
TEST_F(Testing, Case2)
{
EXPECT_EQ(solution.lengthOfLongestSubstring("bbbbb"), 1);
}
TEST_F(Testing, Case3)
{
EXPECT_EQ(solution.lengthOfLongestSubstring("pwwkew"), 3);
}
TEST_F(Testing, Case4) { EXPECT_EQ(solution.lengthOfLongestSubstring(""), 0); }
TEST_F(Testing, Case5) { EXPECT_EQ(solution.lengthOfLongestSubstring(" "), 1); }
TEST_F(Testing, Case6)
{
EXPECT_EQ(solution.lengthOfLongestSubstring("au"), 2);
}
TEST_F(Testing, Case7)
{
EXPECT_EQ(solution.lengthOfLongestSubstring("aab"), 2);
}
TEST_F(Testing, Case8)
{
EXPECT_EQ(solution.lengthOfLongestSubstring("dvdf"), 3);
}
TEST_F(Testing, Case9)
{
EXPECT_EQ(solution.lengthOfLongestSubstring("tmmzuxt"), 5);
}