-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnonRepeatChar.cpp
More file actions
88 lines (77 loc) · 1.85 KB
/
nonRepeatChar.cpp
File metadata and controls
88 lines (77 loc) · 1.85 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
vector<string> strVec;
if (!s.length())
return 0;
string resultStr, longestStr;
// Loop through string and remove the first occurence of repeated char
for (int i = 0; i < s.length(); i++)
{
if (indexOf(resultStr, s[i]))
{
// This vector holds all the non repeating substrings
strVec.push_back(resultStr);
resultStr = removeFirstOccurenceOfChar(resultStr, s[i]);
}
resultStr.push_back(s[i]);
}
strVec.push_back(resultStr);
// if (strVec.empty())
// return resultStr.length();
for (auto i = strVec.begin(); i != strVec.end(); ++i)
{
string currStr = *i;
if (!longestStr.length())
longestStr = currStr;
if (longestStr.length() < currStr.length())
longestStr = *i;
}
return longestStr.length();
// return resultStr.length();
}
private:
bool indexOf(string str, char c)
{
for (int i = 0; i < str.length(); i++)
if (str[i] == c)
return true;
return false;
}
private:
string removeFirstOccurenceOfChar(string str, char c)
{
for (int i = 0; i < str.length(); i++)
if (str[i] == c)
{
// remove this character
str.erase(0, i + 1);
break;
}
return str;
}
};
int main()
{
Solution sol;
// string s = "abcdefalmnopqrst";
// string s = "abcabcbb";
string s = "aab";
// string s = "abcdaefghijklb";
// string s = "pwwkew";
// string s = "cdd";
// string s = "abcdefghh";
// string s = "dvdf";
// string s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
// string s = " ";
// string s = "ohomm";
// string s = "abcdefaxyz";
sol.lengthOfLongestSubstring(s);
return 0;
}