Skip to content

Conversation

nazmulwanted
Copy link
Owner

This is submission for week 2.

@nazmulwanted nazmulwanted requested a review from Sadman007 April 23, 2022 18:35
@Sadman007
Copy link
Collaborator

Hi @nazmulwanted

You need to convert all the solution files with .cpp extensions, not .txt.

@nazmulwanted
Copy link
Owner Author

Hi @nazmulwanted

You need to convert all the solution files with .cpp extensions, not .txt.

sorry... my bad. now converted files to .cpp.

Comment on lines +8 to +21
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;
}
}
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.

Comment on lines +11 to +26
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;
}
}
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.

Comment on lines +8 to +14
unsigned long long getHash(string& str){
unsigned long long hashValue = 1;
for(auto ch: str){
hashValue = hashValue * (257 + (ch - 'a'));
}
return hashValue;
}
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.

Comment on lines +15 to +17
if(numsSize == 1){
return to_string(nums[0]);
}
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!

Comment on lines 17 to 19
if(numsSize == 1){
return to_string(nums[0]);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Again.

Comment on lines +12 to +17
for(int i = 0; i < stringLength; i++){
charCount[s[i] - 'a']++;
}
for(int i = 0; i < stringLength; i++){
charCount[t[i] - 'a']--;
}
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']--;
}

Comment on lines +18 to +35
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();
}
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.

Copy link
Collaborator

@Sadman007 Sadman007 left a comment

Choose a reason for hiding this comment

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

Hi @nazmulwanted

I have completed my review. You can find (if any) suggestions in this thread. Feel free to merge this branch with master/main. You should keep this branch too for future reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants