-
Notifications
You must be signed in to change notification settings - Fork 0
Submission for week 2 #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 17 commits
3141e10
48bde4c
919c839
0b8fb9b
e041892
8a92677
4b3d235
4add03a
3b407af
56b8674
5339ace
1d848aa
089ce10
78ae135
5ab3a33
ecbc9e4
ab96f5c
8b058e2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//Problem 5: Implement strStr() | ||
|
||
//Solution: | ||
|
||
//Time Complexity: O(N) where N is the length of haystack string. | ||
//Space Complexity: O(1) | ||
|
||
int strStr(string haystack, string needle){ | ||
if(needle == "") return 0; | ||
else { | ||
int haystackLength = haystack.length(); | ||
int needleLength = needle.length(); | ||
for(int i = 0; i <= (haystackLength - needleLength); i++){ | ||
if(haystack[i] == needle[0] && | ||
haystack.substr(i, needleLength) == needle){ | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#### Problem 5: Implement strStr() | ||
___ | ||
|
||
##### Solution | ||
|
||
```c++ | ||
//Time Complexity: O(N) where N is the length of haystack string. | ||
//Space Complexity: O(1) | ||
|
||
int strStr(string haystack, string needle){ | ||
if(needle == "") return 0; | ||
else { | ||
int haystackLength = haystack.length(); | ||
int needleLength = needle.length(); | ||
for(int i = 0; i <= (haystackLength - needleLength); i++){ | ||
if(haystack[i] == needle[0] && | ||
haystack.substr(i, needleLength) == needle){ | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//Problem 5: Implement strStr() | ||
|
||
//Solution: | ||
|
||
//Time Complexity: O(N) where N is the length of haystack string. | ||
//Space Complexity: O(1) | ||
|
||
int strStr(string haystack, string needle){ | ||
if(needle == "") return 0; | ||
else { | ||
int haystackLength = haystack.length(); | ||
int needleLength = needle.length(); | ||
for(int i = 0; i <= (haystackLength - needleLength); i++){ | ||
if(haystack[i] == needle[0] && | ||
haystack.substr(i, needleLength) == needle){ | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//Problem 3: Longest Common Prefix (Easy) | ||
|
||
//Solution: | ||
|
||
// Time complexity: O(n * m) | ||
// where, n = number of strings in the vector and | ||
// m = number of common characters ( length of common prefix ) | ||
// Space complexity: O(1) | ||
|
||
string longestCommonPrefix(vector<string>& strs){ | ||
if(strs.size()) == 1){ | ||
return strs[0]; | ||
} | ||
else{ | ||
int numberOfStrings = strs.size(); | ||
string currentLCP = strs[0]; | ||
for(int i = 1; i < numberOfStrings; i++){ | ||
int j; | ||
for(int j = 0; j < strs[i].length(); j++){ | ||
if(strs[i][j] != currentLCP[j]) break; | ||
} | ||
currentLCP = currentLCP.substr(0, j); | ||
} | ||
return currentLCP; | ||
} | ||
} | ||
Comment on lines
+11
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not the optimal one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, is the first |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#### Problem 3: Longest Common Prefix (Easy) | ||
___ | ||
|
||
###### Solution | ||
|
||
```c++ | ||
// Time complexity: O(n * m) | ||
// where, n = number of strings in the vector and | ||
// m = number of common characters ( length of common prefix ) | ||
// Space complexity: O(1) | ||
|
||
string longestCommonPrefix(vector<string>& strs){ | ||
if(strs.size()) == 1){ | ||
return strs[0]; | ||
} | ||
else{ | ||
int numberOfStrings = strs.size(); | ||
string currentLCP = strs[0]; | ||
for(int i = 1; i < numberOfStrings; i++){ | ||
int j; | ||
for(int j = 0; j < strs[i].length(); j++){ | ||
if(strs[i][j] != currentLCP[j]) break; | ||
} | ||
currentLCP = currentLCP.substr(0, j); | ||
} | ||
return currentLCP; | ||
} | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//Problem 3: Longest Common Prefix (Easy) | ||
|
||
//Solution: | ||
|
||
// Time complexity: O(n * m) | ||
// where, n = number of strings in the vector and | ||
// m = number of common characters ( length of common prefix ) | ||
// Space complexity: O(1) | ||
|
||
string longestCommonPrefix(vector<string>& strs){ | ||
if(strs.size()) == 1){ | ||
return strs[0]; | ||
} | ||
else{ | ||
int numberOfStrings = strs.size(); | ||
string currentLCP = strs[0]; | ||
for(int i = 1; i < numberOfStrings; i++){ | ||
int j; | ||
for(int j = 0; j < strs[i].length(); j++){ | ||
if(strs[i][j] != currentLCP[j]) break; | ||
} | ||
currentLCP = currentLCP.substr(0, j); | ||
} | ||
return currentLCP; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//Problem 1: Longest substring without repeating characters (Medium) | ||
|
||
//Solution: | ||
|
||
// Time complexity: O(n) | ||
// where, n = number of characters in the string and | ||
// Space complexity: O(1) | ||
|
||
int lengthOfLongestSubstring(string s){ | ||
vector<int> characterCount(256, 0); | ||
int longestSubStrLen = 0; | ||
int left = 0; | ||
int right = 0; | ||
int duplicateCount = 0; | ||
int strLen = s.length(); | ||
while(right < strLen){ | ||
characterCount[s[right]]++; | ||
if(characterCount[s[right]] > 1){ | ||
duplicateCount++; | ||
} | ||
right++; | ||
while(duplicateCount > 0){ | ||
characterCount[s[left]]--; | ||
if(characterCount[s[left]] == 1){ | ||
duplicateCount--; | ||
} | ||
left++; | ||
} | ||
longestSubStrLen = max(longestSubStrLen, right - left); | ||
} | ||
return longestSubStrLen; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#### Problem 1: Longest substring without repeating characters (Medium) | ||
___ | ||
|
||
###### Solution | ||
|
||
```c++ | ||
// Time complexity: O(n) | ||
// where, n = number of characters in the string and | ||
// Space complexity: O(1) | ||
|
||
int lengthOfLongestSubstring(string s){ | ||
vector<int> characterCount(256, 0); | ||
int longestSubStrLen = 0; | ||
int left = 0; | ||
int right = 0; | ||
int duplicateCount = 0; | ||
int strLen = s.length(); | ||
while(right < strLen){ | ||
characterCount[s[right]]++; | ||
if(characterCount[s[right]] > 1){ | ||
duplicateCount++; | ||
} | ||
right++; | ||
while(duplicateCount > 0){ | ||
characterCount[s[left]]--; | ||
if(characterCount[s[left]] == 1){ | ||
duplicateCount--; | ||
} | ||
left++; | ||
} | ||
longestSubStrLen = max(longestSubStrLen, right - left); | ||
} | ||
return longestSubStrLen; | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//Problem 1: Longest substring without repeating characters (Medium) | ||
|
||
//Solution: | ||
|
||
// Time complexity: O(n) | ||
// where, n = number of characters in the string and | ||
// Space complexity: O(1) | ||
|
||
int lengthOfLongestSubstring(string s){ | ||
vector<int> characterCount(256, 0); | ||
int longestSubStrLen = 0; | ||
int left = 0; | ||
int right = 0; | ||
int duplicateCount = 0; | ||
int strLen = s.length(); | ||
while(right < strLen){ | ||
characterCount[s[right]]++; | ||
if(characterCount[s[right]] > 1){ | ||
duplicateCount++; | ||
} | ||
right++; | ||
while(duplicateCount > 0){ | ||
characterCount[s[left]]--; | ||
if(characterCount[s[left]] == 1){ | ||
duplicateCount--; | ||
} | ||
left++; | ||
} | ||
longestSubStrLen = max(longestSubStrLen, right - left); | ||
} | ||
return longestSubStrLen; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//Problem 12: Decode String (Difficulty: Medium) | ||
|
||
//Solution: | ||
|
||
// Time Complexity: O(N) | ||
// Space Complexity: O(N) | ||
|
||
string decodingString(string& s, int& i){ | ||
string res; | ||
int strLen = s.length(); | ||
while(i < strLen && s[i] != ']'){ | ||
// checking if s[i] is a digit | ||
if(!isdigit(s[i]){ | ||
res += s[i]; | ||
i++; | ||
} | ||
else{ | ||
int num = 0; | ||
while(i < strLen && isdigit(s[i])){ | ||
num = num * 10 + (s[i] - '0'); | ||
i++; | ||
} | ||
i++; // skipping '[' | ||
string tmp = decodingString(s, i); | ||
i++; // skipping ']' | ||
|
||
while(num > 0){ | ||
res += tmp; | ||
num--; | ||
} | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
string decodeString(string s){ | ||
int i = 0; | ||
return decodingString(s, i); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
##### Problem 12: Decode String (Difficulty: Medium) | ||
___ | ||
|
||
#### Solution | ||
|
||
```c++ | ||
// Time Complexity: O(N) | ||
// Space Complexity: O(N) | ||
|
||
string decodingString(string& s, int& i){ | ||
string res; | ||
int strLen = s.length(); | ||
while(i < strLen && s[i] != ']'){ | ||
// checking if s[i] is a digit | ||
if(!isdigit(s[i]){ | ||
res += s[i]; | ||
i++; | ||
} | ||
else{ | ||
int num = 0; | ||
while(i < strLen && isdigit(s[i])){ | ||
num = num * 10 + (s[i] - '0'); | ||
i++; | ||
} | ||
i++; // skipping '[' | ||
string tmp = decodingString(s, i); | ||
i++; // skipping ']' | ||
|
||
while(num > 0){ | ||
res += tmp; | ||
num--; | ||
} | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
string decodeString(string s){ | ||
int i = 0; | ||
return decodingString(s, i); | ||
} | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//Problem 12: Decode String (Difficulty: Medium) | ||
|
||
//Solution: | ||
|
||
// Time Complexity: O(N) | ||
// Space Complexity: O(N) | ||
|
||
string decodingString(string& s, int& i){ | ||
string res; | ||
int strLen = s.length(); | ||
while(i < strLen && s[i] != ']'){ | ||
// checking if s[i] is a digit | ||
if(!isdigit(s[i]){ | ||
res += s[i]; | ||
i++; | ||
} | ||
else{ | ||
int num = 0; | ||
while(i < strLen && isdigit(s[i])){ | ||
num = num * 10 + (s[i] - '0'); | ||
i++; | ||
} | ||
i++; // skipping '[' | ||
string tmp = decodingString(s, i); | ||
i++; // skipping ']' | ||
|
||
while(num > 0){ | ||
res += tmp; | ||
num--; | ||
} | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
string decodeString(string s){ | ||
int i = 0; | ||
return decodingString(s, i); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does the same work, but check the readability and concise-ness.