Skip to content
Open
Show file tree
Hide file tree
Changes from all 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.

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.

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;
}
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);
}
26 changes: 26 additions & 0 deletions week02/group_anagrams.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//Problem 6: Group anagrams. (Difficulty: Medium)

//Solution:

// Time complexity: O(N), where N is number of strings.
// Space complexity: O(N)

unsigned long long getHash(string& str){
unsigned long long hashValue = 1;
for(auto ch: str){
hashValue = hashValue * (257 + (ch - 'a'));
}
return hashValue;
}
Comment on lines +8 to +14
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's better not to use unsigned long long. Refer to my this post.


vector<vector<string>> groupAnagrams(vector<string>& strs){
unordered_map<unsigned long long, vector<string>> lists;
for(auto &str: strs){
lists[getHash(str)].push_back(str);
}
vector<vector<string>> anagramList;
for(auto &item: lists){
anagramList.push_back(item.second);
}
return anagramList;
}
30 changes: 30 additions & 0 deletions week02/largest_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//Problem 8: Largest Number (Difficulty: Medium)

//Solution:

// Time complexity: O(N) + O(NlogN) where N is number of elements
// in the nums vector.
// Space complexity: O(N)

static bool compareFunction(string a, string b){
return a + b > b + a;
}

string largestNumber(vector<int>& nums){
int numsSize = nums.size();
if(numsSize == 1){
return to_string(nums[0]);
}
Comment on lines +15 to +17
Copy link
Collaborator

Choose a reason for hiding this comment

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

Again, same mistake!

else{
vector<string> bucket;
for(int i = 0; i < numsSize; i++){
bucket.push_back(to_string(nums[i]));
}
sort(bucket.begin(), bucket.end(), compareFunction);
string result;
for(int i = 0; i < numsSize; i++){
result += bucket[i];
}
return result[0] == '0'? "0" : result;
}
}
32 changes: 32 additions & 0 deletions week02/minimum_window_substring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//Problem 7: Minimum Window Substring (Difficulty: Hard)

//Solution:

// Time complexity: O(N) where N is string s length.
// Space complexity: O(1)

string minWindow(string s, string t){
vector<int> freqT(130, 0);
for(auto ch: t) freqT[ch]++;

int remCharOfT = t.length();
int left = 0, right = 0, minLength = INT_MAX / 2, startIndx = - 1;
int sLength = s.length();

while(right < sLength){
if(freqT[s[right]] > 0) remCharOfT--;
freqT[s[right]]--;
right++

while(remCharOfT == 0){
if(minLength > right - left){
minLength = right - left;
startIndx = left;
}
if(freqT[s[left]] == 0) remCharOfT++;
freqT[s[left]]++;
left++;
}
}
return startIndx == -1? "" : s.substr(startIndx, minLength);
}
51 changes: 51 additions & 0 deletions week02/string_to_integer_atoi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//Problem 2: String to integer (atoi) (Medium)

//Solution:

// Time complexity: O(N), where N is given string length.
// Space complexity: O(1)

int myAtoi(string s) {
bool isThereNumber = false;
bool isNumberStarted = false;
bool isNegative = false;
int strLength = s.length();
int numberLength = 0;
int i;
for(i = 0; i < strLength; i++){
if(isNumberStarted && (s[i] == '+' || s[i] == '-' || s[i] == ' ') && numberLength == 0){
return 0;
}
else if(isNumberStarted && (s[i] == '+' || s[i] == '-' || s[i] == ' ')){
isNumberStarted = false;
break;
}
else if(s[i] == '+' || s[i] == '-' || (s[i] >= '0' && s[i] <= '9')){
isNumberStarted = true;
isThereNumber = true;
if(s[i] == '-') isNegative = true;
if(s[i] >= '0' && s[i] <= '9') numberLength++;
}
else {
if(isNumberStarted) break;
else if(!isNumberStarted &&
((s[i] >= 'a' && s[i] <= 'z') ||
(s[0] >= 'A' && s[0] <= 'Z') ||
s[0] == '.')) return 0;
}
}

if(isThereNumber) {
string numStr = s.substr(i - numberLength, numberLength);
int result = 0;
for(int j = 0; j < numberLength; j++){
if(result > INT_MAX / 10 ||
(result == INT_MAX / 10 && numStr[j] - '0' > 7)) {
return isNegative? INT_MIN : INT_MAX;
}
result = (result * 10) + (numStr[j] - '0');
}
return isNegative? -1 * result : result;
}
else return 0;
}
22 changes: 22 additions & 0 deletions week02/valid_anagram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//Problem 11: Valid Anagram.

//Solution:

// Time complexity: O(N), where N is the length of the string.
// Space complexity: O(1)

bool isAnagram(string s, string t){
if(s.length() != t.length()) return false;
int charCount[26] = {0};
int stringLength = s.length();
for(int i = 0; i < stringLength; i++){
charCount[s[i] - 'a']++;
}
for(int i = 0; i < stringLength; i++){
charCount[t[i] - 'a']--;
}
Comment on lines +12 to +17
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
for(int i = 0; i < stringLength; i++){
charCount[s[i] - 'a']++;
}
for(int i = 0; i < stringLength; i++){
charCount[t[i] - 'a']--;
}
for(int i = 0; i < stringLength; i++){
charCount[s[i] - 'a']++;
charCount[t[i] - 'a']--;
}

for(int i = 0; i < 26; i++){
if(charCount[i] != 0) return false;
}
return true;
}
40 changes: 40 additions & 0 deletions week02/valid_parentheses.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//Problem 4: Valid Parentheses

//Solution:

//Time complexity: O(N), where N is string length
//Space complexity: O(N)

bool isValid(string s){
int strLength = s.length();
int stackTop = 0;
string myStack;

for(int i = 0; i < strLength; i++){
if(s[i] == '(' || s[i] == '{' || s[i] == '['){
myStack += s[i];
stackTop++;
}
else if(s[i] == ')'){
if(stackTop == 0) return false; // if stack is already empty return false.
top--;
if(myStack[stackTop] != '(') return false;
myStack.pop_back();
}
else if(s[i] == '}'){
if(stackTop == 0) return false; // if stack is already empty return false.
top--;
if(myStack[stackTop] != '{') return false;
myStack.pop_back();
}
else if(s[i] == ']'){
if(stackTop == 0) return false; // if stack is already empty return false.
top--;
if(myStack[stackTop] != '[') return false;
myStack.pop_back();
}
Comment on lines +18 to +35
Copy link
Collaborator

Choose a reason for hiding this comment

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

So many repetitions! See this.

}

if(stackTop == 0) return true;
else return false;
}