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 8, 2023
1 parent 33f1f67 commit 95a79f6
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 19 deletions.
16 changes: 16 additions & 0 deletions leetcode/problems/c/reverse-integer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// by @codeAbinash
// Time : O(1)
// Space : O(1)

#include "limits.h"

int reverse(int x) {
long reversed = 0;
while (x) {
reversed = reversed * 10 + x % 10;
x /= 10;
}
if (reversed > INT_MAX || reversed < INT_MIN)
return 0;
return x < 0 ? -reversed : reversed;
}
39 changes: 20 additions & 19 deletions leetcode/problems/c/reverse-vowels-of-a-string.c
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
// by @codeAbinash
// Time Complexity : O(n)
// Space Complexity : O(1)
// Time : O(n)
// Space : O(1)

int isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
void inline swap(char* a, char* b) {
char tmp = *a;
*a = *b;
*b = tmp;
}

char* reverseVowels(char* s) {
int start = 0, end = strlen(s) - 1;
char tmp;

while (start < end && (s[start] || s[end])) {
if (!isVowel(s[start])) start++;
if (!isVowel(s[end])) end--;
bool inline isVowel(char c) {
return strchr("aeiouAEIOU", c) != NULL;
}

if (isVowel(s[start]) && isVowel(s[end])) {
tmp = s[start];
s[start++] = s[end];
s[end--] = tmp;
}
}
return s;
char* reverseVowels(char* s) {
char* start, * end, tmp;
start = s;
end = s + strlen(s) - 1;
while (start < end) {
while (start < end && !isVowel(*start)) start++;
while (start < end && !isVowel(*end)) end--;
swap(start, end);
start++, end--;
}
return s;
}
23 changes: 23 additions & 0 deletions leetcode/problems/cpp/reverse-integer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// by @codeAbinash
// Time : O(1)
// Space : O(1)

#include "climits"
#include "bits/std_abs.h"

using namespace std;
class Solution {
public:
int reverse(int x) {
bool isNegative = x < 0;
long int reversed = 0;
x = abs(x);
while (x) {
reversed = reversed * 10 + x % 10;
x /= 10;
}
if (reversed > INT_MAX || reversed < INT_MIN)
return 0;
return isNegative ? -reversed : reversed;
}
};
24 changes: 24 additions & 0 deletions leetcode/problems/cpp/reverse-vowels-of-a-string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

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

bool inline isVowel(char c) {
return strchr("aeiouAEIOU", c) != NULL;
}
class Solution {
public:
string reverseVowels(string s) {
int i = 0, j = s.size() - 1;
while (i < j) {
while (i < j && !isVowel(s[i])) i++;
while (i < j && !isVowel(s[j])) j--;
swap(s[i++], s[j--]);
}
return s;
}
};
27 changes: 27 additions & 0 deletions leetcode/problems/cpp/valid-anagram.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// by @codeAbinash
// Time : O(n)
// Space : O(1)

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

class Solution {
public:
bool isAnagram(string s, string t) {
int s_len = s.length();
int t_len = t.length();

if (s_len != t_len) return false;

int hash[26] = { 0 };
for (int i = 0; i < s_len; i++) {
hash[s[i] - 'a']++;
hash[t[i] - 'a']--;
}
// Check if all the elements in the hash are 0
for (int i = 0; i < 26; i++)
if (hash[i] != 0) return false;
return true;
}
};
22 changes: 22 additions & 0 deletions leetcode/problems/cpp/word-pattern.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// by @codeAbinash

#include "iostream"
#include "map"

using namespace std;

class Solution {
public:
bool wordPattern(string pattern, string s) {
map<char, int> p2i;
map<string, int> w2i;
istringstream in(s);
int i = 0, n = pattern.size();
for (string word; in >> word; ++i) {
if (i == n || p2i[pattern[i]] != w2i[word])
return false;
p2i[pattern[i]] = w2i[word] = i + 1;
}
return i == n;
}
};

0 comments on commit 95a79f6

Please sign in to comment.