-
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 all 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,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,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,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
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. It's better not to use |
||
|
||
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; | ||
} |
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
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, 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; | ||
} | ||
} |
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); | ||
} |
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; | ||
} |
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
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.
Suggested change
|
||||||||||||||||||||||
for(int i = 0; i < 26; i++){ | ||||||||||||||||||||||
if(charCount[i] != 0) return false; | ||||||||||||||||||||||
} | ||||||||||||||||||||||
return true; | ||||||||||||||||||||||
} |
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
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. So many repetitions! See this. |
||
} | ||
|
||
if(stackTop == 0) return true; | ||
else return false; | ||
} |
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.