Skip to content

Commit

Permalink
Added some code
Browse files Browse the repository at this point in the history
  • Loading branch information
codeAbinash committed Aug 3, 2023
1 parent 10d3e6f commit d638d75
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
// by @codeAntu
// time complexity: O(n)
// space complexity: O(1)
// by @codeAbinash
// Time : O(n)
// Space : O(1)

#include "string"
using namespace std;

class Solution {
public:
int strStr(string haystack, string needle) {
int ind = 0;
for (int i = 0; i < haystack.size(); i++){
if (haystack[i] == needle[0]){
ind = i;
for (int j = 0; j < needle.size(); j++){
if (haystack[ind] != needle[j]){
ind = 0;
break;
}
ind++;
}
if (ind != 0){
return i;
}
}
int m = haystack.size(), n = needle.size();
if (n == 0) return 0;
for (int i = 0; i <= m - n; i++) {
int j;
for (j = 0; j < n; j++)
if (haystack[i + j] != needle[j]) break;
if (j == n) return i;
}
return -1;
}
Expand Down
21 changes: 13 additions & 8 deletions leetcode/problems/cpp/repeated-substring-pattern.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// by @codeAbinash
// Time Complexity : O(n)
// Space Complexity : O(1n)

#include "vector"
#include "string"
using namespace std;

class Solution {
public:
bool repeatedSubstringPattern(string s) {
int size = s.size();
string str = s.substr(1, size - 1) + s.substr(0, size - 1);
return str.find(s) != -1;
}
public:
bool repeatedSubstringPattern(string s) {
int n = s.length();
for (int i = n / 2; i >= 1; i--)
if (n % i == 0)
if (s.substr(0, n - i) == s.substr(i))
return true;
return false;
}
};
37 changes: 37 additions & 0 deletions leetcode/problems/cpp/set-matrix-zeroes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// by @codeAbinash

#include "vector"
using namespace std;

class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int row = matrix.size();
int col = matrix[0].size();
bool isRowZero = false;

// Check each element and set the first row and col to 0
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
if (matrix[i][j] == 0) {
matrix[0][j] = 0;
if (i > 0)
matrix[i][0] = 0;
else
isRowZero = true;
}
// Set Zero
for (int i = 1; i < row; i++)
for (int j = 1; j < col; j++)
if (matrix[i][0] == 0 || matrix[0][j] == 0)
matrix[i][j] = 0;

if (matrix[0][0] == 0)
for (int i = 0; i < row; i++)
matrix[i][0] = 0;

if (isRowZero)
for (int j = 0; j < col; j++)
matrix[0][j] = 0;
}
};
37 changes: 37 additions & 0 deletions leetcode/problems/cpp/spiral-matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// by @codeAbinash
// Time : O(n)
// Space : O(n)

#include "vector"
using namespace std;

class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int left = 0, right = matrix[0].size();
int top = 0, bottom = matrix.size();
vector<int> ans;

while (top < bottom && left < right) {
for (int i = left; i < right; i++)
ans.push_back(matrix[top][i]);
top++;

for (int i = top; i < bottom; i++)
ans.push_back(matrix[i][right - 1]);
right--;

if (!(left < right && top < bottom))
break;

for (int i = right - 1; i >= left; i--)
ans.push_back(matrix[bottom - 1][i]);
bottom--;

for (int i = bottom - 1; i >= top; i--)
ans.push_back(matrix[i][left]);
left++;
}
return ans;
}
};

0 comments on commit d638d75

Please sign in to comment.