Skip to content

Commit

Permalink
Rename capital files
Browse files Browse the repository at this point in the history
  • Loading branch information
neetcode-gh committed Jan 15, 2023
1 parent f3afb84 commit 0207724
Show file tree
Hide file tree
Showing 33 changed files with 862 additions and 0 deletions.
21 changes: 21 additions & 0 deletions c/0014-longest-common-prefix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
char * longestCommonPrefix(char ** strs, int strsSize){
int commonPrefixCount = 0;
int firstStrSize = strlen(strs[0]);

for(int i = 0; i < firstStrSize; i++)
{
for(int s = 1; s < strsSize; s++)
{
if(i == strlen(strs[s]) || strs[0][i] != strs[s][i])
{
// Add null terminator after the last common prefix char
strs[0][commonPrefixCount] = '\0';
return strs[0];
}
}

commonPrefixCount++;
}

return strs[0];
}
72 changes: 72 additions & 0 deletions c/0290-word-pattern.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
typedef struct charToWord {
int ch; /* we'll use this field as the key */
char* word;
UT_hash_handle hh; /* makes this structure hashable */
}charToWord;

typedef struct wordToChar {
char* word; /* we'll use this field as the key */
int ch;
UT_hash_handle hh; /* makes this structure hashable */
}wordToChar;

bool wordPattern(char * pattern, char * s){
charToWord* charToWordMap = NULL;
wordToChar* wordToCharMap = NULL;
char* word = NULL;

// Get the first word
word = strtok(s, " ");

for(size_t i = 0; i < strlen(pattern); i++)
{
charToWord* charToWordEntry = NULL;
wordToChar* wordToCharEntry = NULL;
int ch = pattern[i];

// If there is no words left (pattern > s)
if(word == NULL)
{
return false;
}

HASH_FIND_INT(charToWordMap, &ch, charToWordEntry);
HASH_FIND_STR(wordToCharMap, word, wordToCharEntry);

// If the char does exist in the map and the mapping is not the current word
if(charToWordEntry && strcmp(charToWordEntry->word, word) != 0)
{
return false;
}

// If the word does exist in the map and the mapping is not the current char
if(wordToCharEntry && wordToCharEntry->ch != ch)
{
return false;
}

/* Setup hash entries */
charToWordEntry = (charToWord*)malloc(sizeof(charToWord));
charToWordEntry->ch = ch;
charToWordEntry->word = word;

wordToCharEntry = (wordToChar*)malloc(sizeof(wordToChar));
wordToCharEntry->word = word;
wordToCharEntry->ch = ch;

/* Add entries to the hashes */
HASH_ADD_INT(charToWordMap, ch, charToWordEntry);
HASH_ADD_STR(wordToCharMap, word, wordToCharEntry);

// Move to the next word
word = strtok(NULL, " ");
}

// If there is any words left (s > pattern)
if(word != NULL)
{
return false;
}

return true;
}
25 changes: 25 additions & 0 deletions c/0448-find-all-numbers-disappeared-in-an-array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* findDisappearedNumbers(int* nums, int numsSize, int* returnSize){
int* disappearedNumbers = (int*)malloc(numsSize * sizeof(int)); // Allocate space for the worst case
*returnSize = 0; // Points to the first empty index in the array

// Mark available numbers as negative
for(int i = 0; i < numsSize; i++)
{
int index = abs(nums[i]);
nums[index - 1] = -1 * abs(nums[index - 1]);
}

// Find unmarked numbers (disappeared numbers)
for(int i = 0; i < numsSize; i++)
{
if(nums[i] > 0)
{
disappearedNumbers[(*returnSize)++] = i + 1;
}
}

return disappearedNumbers;
}
45 changes: 45 additions & 0 deletions c/0554-brick-wall.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#define max(x, y) ((x) > (y) ? (x) : (y))

typedef struct hash_entry {
int position; /* we'll use this field as the key */
int gapCount;
UT_hash_handle hh; /* makes this structure hashable */
} hash_entry;

int leastBricks(int** wall, int wallSize, int* wallColSize){
hash_entry* wallGapCountMap = NULL;

for(int r = 0; r < wallSize; r++)
{
int position = 0;
for(int b = 0; b < *(wallColSize + r) - 1; b++)
{
position += wall[r][b];

hash_entry* retrievedMapEntry;
HASH_FIND_INT(wallGapCountMap, &position, retrievedMapEntry);

// If the position already exists in the map then increment its gap count
if(retrievedMapEntry)
{
retrievedMapEntry->gapCount += 1;
}
else
{
// If the position doesn't exist in the map then create a new map entry for it and add it to the map
hash_entry* mapEntryToAdd = (hash_entry*)malloc(sizeof(hash_entry));
mapEntryToAdd->position = position;
mapEntryToAdd->gapCount = 1;
HASH_ADD_INT(wallGapCountMap, position, mapEntryToAdd);
}
}
}

int maxGap = 0;
for (hash_entry* retrievedMapEntry = wallGapCountMap; retrievedMapEntry != NULL; retrievedMapEntry = retrievedMapEntry->hh.next)
{
maxGap = max(maxGap, retrievedMapEntry->gapCount);
}

return wallSize - maxGap;
}
15 changes: 15 additions & 0 deletions cpp/0026-remove-duplicates-from-sorted-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int left = 1;

for(int right = 1; right < nums.size(); right++){
if(nums[right] != nums[right - 1]){
nums[left] = nums[right];
left++;
}
}

return left;
}
};
17 changes: 17 additions & 0 deletions cpp/0027-remove-element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int n=nums.size();
int count=0;
for(int i=0;i<n;i++)
{
if(nums[i]!=val)
{
swap(nums[i],nums[count]);
count++;
}
}

return count;
}
};
17 changes: 17 additions & 0 deletions cpp/0028-find-the-index-of-the-first-occurrence-in-a-string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int strStr(string haystack, string needle) {
if(haystack.size()<needle.size()) return -1;
int found=0;
for(int i=0;i<haystack.size()-needle.size()+1;i++){
if(haystack[i]==needle[0]){
found=1;
for(int j=1;j<needle.size();j++){
if(haystack[i+j]!=needle[j]){
found=0;break;
}
}if(found==1) return i;
}
}return -1;
}
};
28 changes: 28 additions & 0 deletions cpp/0067-add-binary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public:
string addBinary(string a, string b) {
string res;
int maxLen = a.size() > b.size() ? a.size() : b.size();
unsigned int carry = 0;

for(int i = 0; i < maxLen; i++)
{
unsigned int bitA = i < a.size() ? a[a.size() - i - 1] - '0' : 0;
unsigned int bitB = i < b.size() ? b[b.size() - i - 1] - '0' : 0;

unsigned int total = bitA + bitB + carry;
char sum = '0' + total % 2;
carry = total / 2;

// Add to the beginning of the string
res.insert(0, 1, sum);
}

if(carry)
{
res.insert(0, 1, '1');
}

return res;
}
};
22 changes: 22 additions & 0 deletions cpp/0075-sort-colors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
void sortColors(vector<int>& nums) {
int p1=0,p2=nums.size()-1;
for(int i=p1;i<=p2;i++)
{
if(nums[i]==0)
{
swap(nums[i],nums[p1]);
p1++;
}
if(nums[i]==2)
{
swap(nums[i],nums[p2]);
p2--;
i--;
}
}


}
};
31 changes: 31 additions & 0 deletions cpp/0088-merge-sorted-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int j=0;
int i=0;
if(n==0) return;
if(m==0)
{
for(int i = 0; i < n; i++){
nums1[i] = nums2[i];
} return;
}
while(i<m)
{
if(nums1[i]>nums2[j])
{
swap(nums1[i],nums2[j]);
sort(nums2.begin(),nums2.end());
}
i++;
}
j=0;
while(i<m+n)
{
nums1[i] = nums2[j];
j++;
i++;
}

}
};
23 changes: 23 additions & 0 deletions cpp/0205-isomorphic-strings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*Given two strings s and t, determine if they are isomorphic.
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
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.
*/
class Solution {
public:
bool isIsomorphic(string s, string t) {
unordered_map<char,vector<int>>m1;
unordered_map<char,vector<int>>m2;
for(int i=0;i<s.length();i++){
m1[s[i]].push_back(i);
m2[t[i]].push_back(i);

if(m1[s[i]]!=m2[t[i]])
return false;
}
return true;

}
};
13 changes: 13 additions & 0 deletions cpp/0344-reverse-string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public:
void reverseString(vector<char>& s) {
int left = 0, right = s.size() - 1;

while (left < right){
swap(s[left], s[right]);

left++;
right--;
}
}
};
18 changes: 18 additions & 0 deletions cpp/0665-non-decreasing-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
bool checkPossibility(vector<int>& nums) {
int count = 0;
for(int i=1;i<nums.size();i++){
if(nums[i-1]>nums[i]){
count++;
if(i>=2&&nums[i-2]>nums[i]){
nums[i]=nums[i-1];
}
else{
nums[i-1]=nums[i];
}
}
}
return count<=1;
}
};
15 changes: 15 additions & 0 deletions cpp/1985-find-the-kth-largest-integer-in-the-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
private:
static bool st(string &a,string &b){
if(a.size()==b.size()) return a<b;
return a.size()<b.size();
}


public:
string kthLargestNumber(vector<string>& nums, int k) {
sort(nums.begin(),nums.end(),st);
return nums[nums.size()-k];

}
};
Loading

0 comments on commit 0207724

Please sign in to comment.