-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33f1f67
commit 95a79f6
Showing
6 changed files
with
132 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |