Skip to content
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions week02/Implement_strStr.cpp
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;
}
}
Comment on lines +8 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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;
}
}
int strStr(string haystack, string needle){
for(int i = 0; i <= (haystack.length()- needle.length()); i++){
if(haystack.substr(i, needleLength) == needle){
return i;
}
}
return -1;
}

It does the same work, but check the readability and concise-ness.

24 changes: 24 additions & 0 deletions week02/Implement_strStr.md
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;
}
}
```
21 changes: 21 additions & 0 deletions week02/Implement_strStr.txt
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;
}
}
26 changes: 26 additions & 0 deletions week02/Longest_Common_Prefix.cpp
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the optimal one.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, is the first if condition necessary? Don't handle special cases too often in every program you write. It's not a good practice.

29 changes: 29 additions & 0 deletions week02/Longest_Common_Prefix.md
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;
}
}
```
26 changes: 26 additions & 0 deletions week02/Longest_Common_Prefix.txt
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;
}
}
32 changes: 32 additions & 0 deletions week02/Longest_substring_length_without_repeating_characters.cpp
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;
}
35 changes: 35 additions & 0 deletions week02/Longest_substring_length_without_repeating_characters.md
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;
}
```
32 changes: 32 additions & 0 deletions week02/Longest_substring_length_without_repeating_characters.txt
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;
}
39 changes: 39 additions & 0 deletions week02/decode_string.cpp
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);
}
42 changes: 42 additions & 0 deletions week02/decode_string.md
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);
}
```
39 changes: 39 additions & 0 deletions week02/decode_string.txt
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);
}
Loading