Skip to content

Commit 0207724

Browse files
committed
Rename capital files
1 parent f3afb84 commit 0207724

33 files changed

+862
-0
lines changed

c/0014-longest-common-prefix.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
char * longestCommonPrefix(char ** strs, int strsSize){
2+
int commonPrefixCount = 0;
3+
int firstStrSize = strlen(strs[0]);
4+
5+
for(int i = 0; i < firstStrSize; i++)
6+
{
7+
for(int s = 1; s < strsSize; s++)
8+
{
9+
if(i == strlen(strs[s]) || strs[0][i] != strs[s][i])
10+
{
11+
// Add null terminator after the last common prefix char
12+
strs[0][commonPrefixCount] = '\0';
13+
return strs[0];
14+
}
15+
}
16+
17+
commonPrefixCount++;
18+
}
19+
20+
return strs[0];
21+
}

c/0290-word-pattern.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
typedef struct charToWord {
2+
int ch; /* we'll use this field as the key */
3+
char* word;
4+
UT_hash_handle hh; /* makes this structure hashable */
5+
}charToWord;
6+
7+
typedef struct wordToChar {
8+
char* word; /* we'll use this field as the key */
9+
int ch;
10+
UT_hash_handle hh; /* makes this structure hashable */
11+
}wordToChar;
12+
13+
bool wordPattern(char * pattern, char * s){
14+
charToWord* charToWordMap = NULL;
15+
wordToChar* wordToCharMap = NULL;
16+
char* word = NULL;
17+
18+
// Get the first word
19+
word = strtok(s, " ");
20+
21+
for(size_t i = 0; i < strlen(pattern); i++)
22+
{
23+
charToWord* charToWordEntry = NULL;
24+
wordToChar* wordToCharEntry = NULL;
25+
int ch = pattern[i];
26+
27+
// If there is no words left (pattern > s)
28+
if(word == NULL)
29+
{
30+
return false;
31+
}
32+
33+
HASH_FIND_INT(charToWordMap, &ch, charToWordEntry);
34+
HASH_FIND_STR(wordToCharMap, word, wordToCharEntry);
35+
36+
// If the char does exist in the map and the mapping is not the current word
37+
if(charToWordEntry && strcmp(charToWordEntry->word, word) != 0)
38+
{
39+
return false;
40+
}
41+
42+
// If the word does exist in the map and the mapping is not the current char
43+
if(wordToCharEntry && wordToCharEntry->ch != ch)
44+
{
45+
return false;
46+
}
47+
48+
/* Setup hash entries */
49+
charToWordEntry = (charToWord*)malloc(sizeof(charToWord));
50+
charToWordEntry->ch = ch;
51+
charToWordEntry->word = word;
52+
53+
wordToCharEntry = (wordToChar*)malloc(sizeof(wordToChar));
54+
wordToCharEntry->word = word;
55+
wordToCharEntry->ch = ch;
56+
57+
/* Add entries to the hashes */
58+
HASH_ADD_INT(charToWordMap, ch, charToWordEntry);
59+
HASH_ADD_STR(wordToCharMap, word, wordToCharEntry);
60+
61+
// Move to the next word
62+
word = strtok(NULL, " ");
63+
}
64+
65+
// If there is any words left (s > pattern)
66+
if(word != NULL)
67+
{
68+
return false;
69+
}
70+
71+
return true;
72+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Note: The returned array must be malloced, assume caller calls free().
3+
*/
4+
int* findDisappearedNumbers(int* nums, int numsSize, int* returnSize){
5+
int* disappearedNumbers = (int*)malloc(numsSize * sizeof(int)); // Allocate space for the worst case
6+
*returnSize = 0; // Points to the first empty index in the array
7+
8+
// Mark available numbers as negative
9+
for(int i = 0; i < numsSize; i++)
10+
{
11+
int index = abs(nums[i]);
12+
nums[index - 1] = -1 * abs(nums[index - 1]);
13+
}
14+
15+
// Find unmarked numbers (disappeared numbers)
16+
for(int i = 0; i < numsSize; i++)
17+
{
18+
if(nums[i] > 0)
19+
{
20+
disappearedNumbers[(*returnSize)++] = i + 1;
21+
}
22+
}
23+
24+
return disappearedNumbers;
25+
}

c/0554-brick-wall.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#define max(x, y) ((x) > (y) ? (x) : (y))
2+
3+
typedef struct hash_entry {
4+
int position; /* we'll use this field as the key */
5+
int gapCount;
6+
UT_hash_handle hh; /* makes this structure hashable */
7+
} hash_entry;
8+
9+
int leastBricks(int** wall, int wallSize, int* wallColSize){
10+
hash_entry* wallGapCountMap = NULL;
11+
12+
for(int r = 0; r < wallSize; r++)
13+
{
14+
int position = 0;
15+
for(int b = 0; b < *(wallColSize + r) - 1; b++)
16+
{
17+
position += wall[r][b];
18+
19+
hash_entry* retrievedMapEntry;
20+
HASH_FIND_INT(wallGapCountMap, &position, retrievedMapEntry);
21+
22+
// If the position already exists in the map then increment its gap count
23+
if(retrievedMapEntry)
24+
{
25+
retrievedMapEntry->gapCount += 1;
26+
}
27+
else
28+
{
29+
// If the position doesn't exist in the map then create a new map entry for it and add it to the map
30+
hash_entry* mapEntryToAdd = (hash_entry*)malloc(sizeof(hash_entry));
31+
mapEntryToAdd->position = position;
32+
mapEntryToAdd->gapCount = 1;
33+
HASH_ADD_INT(wallGapCountMap, position, mapEntryToAdd);
34+
}
35+
}
36+
}
37+
38+
int maxGap = 0;
39+
for (hash_entry* retrievedMapEntry = wallGapCountMap; retrievedMapEntry != NULL; retrievedMapEntry = retrievedMapEntry->hh.next)
40+
{
41+
maxGap = max(maxGap, retrievedMapEntry->gapCount);
42+
}
43+
44+
return wallSize - maxGap;
45+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int removeDuplicates(vector<int>& nums) {
4+
int left = 1;
5+
6+
for(int right = 1; right < nums.size(); right++){
7+
if(nums[right] != nums[right - 1]){
8+
nums[left] = nums[right];
9+
left++;
10+
}
11+
}
12+
13+
return left;
14+
}
15+
};

cpp/0027-remove-element.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int removeElement(vector<int>& nums, int val) {
4+
int n=nums.size();
5+
int count=0;
6+
for(int i=0;i<n;i++)
7+
{
8+
if(nums[i]!=val)
9+
{
10+
swap(nums[i],nums[count]);
11+
count++;
12+
}
13+
}
14+
15+
return count;
16+
}
17+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int strStr(string haystack, string needle) {
4+
if(haystack.size()<needle.size()) return -1;
5+
int found=0;
6+
for(int i=0;i<haystack.size()-needle.size()+1;i++){
7+
if(haystack[i]==needle[0]){
8+
found=1;
9+
for(int j=1;j<needle.size();j++){
10+
if(haystack[i+j]!=needle[j]){
11+
found=0;break;
12+
}
13+
}if(found==1) return i;
14+
}
15+
}return -1;
16+
}
17+
};

cpp/0067-add-binary.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
string addBinary(string a, string b) {
4+
string res;
5+
int maxLen = a.size() > b.size() ? a.size() : b.size();
6+
unsigned int carry = 0;
7+
8+
for(int i = 0; i < maxLen; i++)
9+
{
10+
unsigned int bitA = i < a.size() ? a[a.size() - i - 1] - '0' : 0;
11+
unsigned int bitB = i < b.size() ? b[b.size() - i - 1] - '0' : 0;
12+
13+
unsigned int total = bitA + bitB + carry;
14+
char sum = '0' + total % 2;
15+
carry = total / 2;
16+
17+
// Add to the beginning of the string
18+
res.insert(0, 1, sum);
19+
}
20+
21+
if(carry)
22+
{
23+
res.insert(0, 1, '1');
24+
}
25+
26+
return res;
27+
}
28+
};

cpp/0075-sort-colors.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
void sortColors(vector<int>& nums) {
4+
int p1=0,p2=nums.size()-1;
5+
for(int i=p1;i<=p2;i++)
6+
{
7+
if(nums[i]==0)
8+
{
9+
swap(nums[i],nums[p1]);
10+
p1++;
11+
}
12+
if(nums[i]==2)
13+
{
14+
swap(nums[i],nums[p2]);
15+
p2--;
16+
i--;
17+
}
18+
}
19+
20+
21+
}
22+
};

cpp/0088-merge-sorted-array.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
4+
int j=0;
5+
int i=0;
6+
if(n==0) return;
7+
if(m==0)
8+
{
9+
for(int i = 0; i < n; i++){
10+
nums1[i] = nums2[i];
11+
} return;
12+
}
13+
while(i<m)
14+
{
15+
if(nums1[i]>nums2[j])
16+
{
17+
swap(nums1[i],nums2[j]);
18+
sort(nums2.begin(),nums2.end());
19+
}
20+
i++;
21+
}
22+
j=0;
23+
while(i<m+n)
24+
{
25+
nums1[i] = nums2[j];
26+
j++;
27+
i++;
28+
}
29+
30+
}
31+
};

cpp/0205-isomorphic-strings.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*Given two strings s and t, determine if they are isomorphic.
2+
3+
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
4+
5+
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
6+
7+
*/
8+
class Solution {
9+
public:
10+
bool isIsomorphic(string s, string t) {
11+
unordered_map<char,vector<int>>m1;
12+
unordered_map<char,vector<int>>m2;
13+
for(int i=0;i<s.length();i++){
14+
m1[s[i]].push_back(i);
15+
m2[t[i]].push_back(i);
16+
17+
if(m1[s[i]]!=m2[t[i]])
18+
return false;
19+
}
20+
return true;
21+
22+
}
23+
};

cpp/0344-reverse-string.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
void reverseString(vector<char>& s) {
4+
int left = 0, right = s.size() - 1;
5+
6+
while (left < right){
7+
swap(s[left], s[right]);
8+
9+
left++;
10+
right--;
11+
}
12+
}
13+
};

cpp/0665-non-decreasing-array.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
bool checkPossibility(vector<int>& nums) {
4+
int count = 0;
5+
for(int i=1;i<nums.size();i++){
6+
if(nums[i-1]>nums[i]){
7+
count++;
8+
if(i>=2&&nums[i-2]>nums[i]){
9+
nums[i]=nums[i-1];
10+
}
11+
else{
12+
nums[i-1]=nums[i];
13+
}
14+
}
15+
}
16+
return count<=1;
17+
}
18+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
private:
3+
static bool st(string &a,string &b){
4+
if(a.size()==b.size()) return a<b;
5+
return a.size()<b.size();
6+
}
7+
8+
9+
public:
10+
string kthLargestNumber(vector<string>& nums, int k) {
11+
sort(nums.begin(),nums.end(),st);
12+
return nums[nums.size()-k];
13+
14+
}
15+
};

0 commit comments

Comments
 (0)