Skip to content

Commit 5e29184

Browse files
committed
add solutions
1 parent aeecc6b commit 5e29184

File tree

11 files changed

+176
-1
lines changed

11 files changed

+176
-1
lines changed

problems/1160.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/find-words-that-can-be-formed-by-characters", "name": "Find Words That Can Be Formed by Characters", "difficulty": "Easy", "statement": "

You are given an array of strings words and a string chars.

\n\n

A string is good if it can be formed by characters from chars (each character can only be used once).

\n\n

Return the sum of lengths of all good strings in words.

\n\n

 

\n\n

Example 1:

\n\n
Input: words = [\"cat\",\"bt\",\"hat\",\"tree\"], chars = \"atach\"\nOutput: 6\nExplanation: \nThe strings that can be formed are \"cat\" and \"hat\" so the answer is 3 + 3 = 6.\n
\n\n

Example 2:

\n\n
Input: words = [\"hello\",\"world\",\"leetcode\"], chars = \"welldonehoneyr\"\nOutput: 10\nExplanation: \nThe strings that can be formed are \"hello\" and \"world\" so the answer is 5 + 5 = 10.\n
\n\n

 

\n\n

Note:

\n\n
    \n\t
  1. 1 <= words.length <= 1000
  2. \n\t
  3. 1 <= words[i].length, chars.length <= 100
  4. \n\t
  5. All strings contain lowercase English letters only.
  6. \n
", "language": "cpp", "solution": "#include \n#include \n\nusing namespace std;\n\nclass Solution\n{\npublic:\n int countCharacters(vector &words, string chars)\n {\n\n int sum = 0;\n int *count = (int *)malloc(sizeof(int) * 26);\n fill_n(count, 26, 0);\n\n for (char ch : chars)\n {\n ++count[(int)(ch)-97];\n }\n\n for (string s : words)\n {\n int *count_t = (int *)malloc(sizeof(int) * 26);\n fill_n(count_t, 26, 0);\n\n for (char ch : s)\n {\n ++count_t[(int)(ch)-97];\n }\n\n int check = 1;\n\n for(int i = 0; i < 26; i++) {\n if (count_t[i] > count[i]) {\n check = 0;\n break;\n }\n }\n\n if (check) sum += s.length();\n }\n return sum;\n }\n};"}

problems/509.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/fibonacci-number", "name": "Fibonacci Number", "difficulty": "Easy", "statement": "

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

\n\n
F(0) = 0,   F(1) = 1\nF(N) = F(N - 1) + F(N - 2), for N > 1.\n
\n\n

Given N, calculate F(N).

\n\n

 

\n\n

Example 1:

\n\n
Input: 2\nOutput: 1\nExplanation: F(2) = F(1) + F(0) = 1 + 0 = 1.\n
\n\n

Example 2:

\n\n
Input: 3\nOutput: 2\nExplanation: F(3) = F(2) + F(1) = 1 + 1 = 2.\n
\n\n

Example 3:

\n\n
Input: 4\nOutput: 3\nExplanation: F(4) = F(3) + F(2) = 2 + 1 = 3.\n
\n\n

 

\n\n

Note:

\n\n

0 \u2264 N \u2264 30.

\n
", "language": "cpp", "solution": "#include \n#include \n\nusing namespace std;\n\nclass Solution\n{\npublic:\n int fib(int N)\n {\n int *arr = (int *)malloc(sizeof(int) * N + 1);\n\n int n = N;\n if (n == 0) return 0;\n if (n == 1) return 1;\n\n arr[0] = 0;\n arr[1] = 1;\n\n for(int i = 2; i <= N; i++)\n arr[i] = arr[i-1] + arr[i-2];\n\n return arr[N];\n\n }\n};"}

problems/821.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/shortest-distance-to-a-character", "name": "Shortest Distance to a Character", "difficulty": "Easy", "statement": "

Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string.

\n\n

Example 1:

\n\n
Input: S = \"loveleetcode\", C = 'e'\nOutput: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]\n
\n\n

 

\n\n

Note:

\n\n
    \n\t
  1. S string length is in [1, 10000].
  2. \n\t
  3. C is a single character, and guaranteed to be in string S.
  4. \n\t
  5. All letters in S and C are lowercase.
  6. \n
\n
", "language": "cpp", "solution": "#include \n#include \n\nusing namespace std;\n\nclass Solution\n{\npublic:\n vector shortestToChar(string S, char C)\n {\n vector poss;\n int n = S.length();\n for (int i = 0; i < n; i++)\n {\n if (S[i] == C)\n {\n poss.push_back(i);\n }\n }\n\n vector out;\n\n int occ = poss.size();\n\n if (occ)\n {\n for (int i = 0; i <= poss[0]; ++i)\n out.push_back(poss[0] - i);\n }\n for (int i = 0; i < occ - 1; i++)\n {\n for (int j = poss[i] + 1; j <= poss[i + 1]; ++j)\n {\n out.push_back(min((j - poss[i]), (poss[i + 1] - j)));\n }\n }\n\n if (occ)\n {\n for (int i = poss[occ - 1] + 1; i < n; ++i)\n out.push_back(i - poss[occ - 1]);\n }\n\n return out;\n }\n};"}

problems/929.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/unique-email-addresses", "name": "Unique Email Addresses", "difficulty": "Easy", "statement": "

Every email consists of a local name and a domain name, separated by the @ sign.

\n\n

For example, in [email protected]alice is the local name, and leetcode.com is the domain name.

\n\n

Besides lowercase letters, these emails may contain '.'s or '+'s.

\n\n

If you add periods ('.') between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name.  For example, \"[email protected]\" and \"[email protected]\" forward to the same email address.  (Note that this rule does not apply for domain names.)

\n\n

If you add a plus ('+') in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered, for example [email protected] will be forwarded to [email protected].  (Again, this rule does not apply for domain names.)

\n\n

It is possible to use both of these rules at the same time.

\n\n

Given a list of emails, we send one email to each address in the list.  How many different addresses actually receive mails? 

\n\n

 

\n\n
\n

Example 1:

\n\n
Input: [\"[email protected]\",\"[email protected]\",\"[email protected]\"]\nOutput: 2\nExplanation: \"[email protected]\" and \"[email protected]\" actually receive mails\n
\n\n

 

\n\n

Note:

\n\n
    \n\t
  • 1 <= emails[i].length <= 100
  • \n\t
  • 1 <= emails.length <= 100
  • \n\t
  • Each emails[i] contains exactly one '@' character.
  • \n\t
  • All local and domain names are non-empty.
  • \n\t
  • Local names do not start with a '+' character.
  • \n
\n
\n
", "language": "cpp", "solution": "#include \n#include \n\nusing namespace std;\n\nclass Solution\n{\npublic:\n int numUniqueEmails(vector &emails)\n {\n\n set track;\n for (string email : emails)\n {\n stringstream processed;\n int plusenc = 0;\n int atenc = 0;\n for(char ch: email) {\n if (ch == '.' && !atenc) continue;\n if (ch == '+') plusenc = 1;\n if (ch == '@') atenc = 1;\n if (plusenc && !atenc) continue;\n processed << ch;\n }\n\n track.insert(processed.str());\n }\n\n return track.size();\n }\n};"}

problems/965.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"link": "https://leetcode.com/problems/univalued-binary-tree", "name": "Univalued Binary Tree", "difficulty": "Easy", "statement": "

A binary tree is univalued if every node in the tree has the same value.

\n\n

Return true if and only if the given tree is univalued.

\n\n

 

\n\n

Example 1:

\n\"\"\n
Input: [1,1,1,1,1,null,1]\nOutput: true\n
\n\n
\n

Example 2:

\n\"\"\n
Input: [2,2,2,5,2]\nOutput: false\n
\n
\n\n

 

\n\n

Note:

\n\n
    \n\t
  1. The number of nodes in the given tree will be in the range [1, 100].
  2. \n\t
  3. Each node's value will be an integer in the range [0, 99].
  4. \n
\n
", "language": "c", "solution": "\nstruct TreeNode\n{\n int val;\n struct TreeNode *left;\n struct TreeNode *right;\n};\n\nbool isUnivalTree(struct TreeNode *root)\n{\n if(!root) return true;\n if(root->left) {\n if (root->left->val != root->val) return false;\n if (!isUnivalTree(root->left)) return false;\n }\n if(root->right) {\n if (root->right->val != root->val) return false;\n if (!isUnivalTree(root->right)) return false;\n }\n\n return true;\n\n}"}

solutions/easy/1160.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <iostream>
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
class Solution
7+
{
8+
public:
9+
int countCharacters(vector &words, string chars)
10+
{
11+
12+
int sum = 0;
13+
int *count = (int *)malloc(sizeof(int) * 26);
14+
fill_n(count, 26, 0);
15+
16+
for (char ch : chars)
17+
{
18+
++count[(int)(ch)-97];
19+
}
20+
21+
for (string s : words)
22+
{
23+
int *count_t = (int *)malloc(sizeof(int) * 26);
24+
fill_n(count_t, 26, 0);
25+
26+
for (char ch : s)
27+
{
28+
++count_t[(int)(ch)-97];
29+
}
30+
31+
int check = 1;
32+
33+
for(int i = 0; i < 26; i++) {
34+
if (count_t[i] > count[i]) {
35+
check = 0;
36+
break;
37+
}
38+
}
39+
40+
if (check) sum += s.length();
41+
}
42+
return sum;
43+
}
44+
};

solutions/easy/509.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
class Solution
7+
{
8+
public:
9+
int fib(int N)
10+
{
11+
int *arr = (int *)malloc(sizeof(int) * N + 1);
12+
13+
int n = N;
14+
if (n == 0) return 0;
15+
if (n == 1) return 1;
16+
17+
arr[0] = 0;
18+
arr[1] = 1;
19+
20+
for(int i = 2; i <= N; i++)
21+
arr[i] = arr[i-1] + arr[i-2];
22+
23+
return arr[N];
24+
25+
}
26+
};

solutions/easy/821.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
class Solution
7+
{
8+
public:
9+
vector<int> shortestToChar(string S, char C)
10+
{
11+
vector<int> poss;
12+
int n = S.length();
13+
for (int i = 0; i < n; i++)
14+
{
15+
if (S[i] == C)
16+
{
17+
poss.push_back(i);
18+
}
19+
}
20+
21+
vector<int> out;
22+
23+
int occ = poss.size();
24+
25+
if (occ)
26+
{
27+
for (int i = 0; i <= poss[0]; ++i)
28+
out.push_back(poss[0] - i);
29+
}
30+
for (int i = 0; i < occ - 1; i++)
31+
{
32+
for (int j = poss[i] + 1; j <= poss[i + 1]; ++j)
33+
{
34+
out.push_back(min((j - poss[i]), (poss[i + 1] - j)));
35+
}
36+
}
37+
38+
if (occ)
39+
{
40+
for (int i = poss[occ - 1] + 1; i < n; ++i)
41+
out.push_back(i - poss[occ - 1]);
42+
}
43+
44+
return out;
45+
}
46+
};

solutions/easy/929.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <iostream>
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
class Solution
7+
{
8+
public:
9+
int numUniqueEmails(vector &emails)
10+
{
11+
12+
set track;
13+
for (string email : emails)
14+
{
15+
stringstream processed;
16+
int plusenc = 0;
17+
int atenc = 0;
18+
for(char ch: email) {
19+
if (ch == '.' && !atenc) continue;
20+
if (ch == '+') plusenc = 1;
21+
if (ch == '@') atenc = 1;
22+
if (plusenc && !atenc) continue;
23+
processed << ch;
24+
}
25+
26+
track.insert(processed.str());
27+
}
28+
29+
return track.size();
30+
}
31+
};

solutions/easy/965.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
struct TreeNode
3+
{
4+
int val;
5+
struct TreeNode *left;
6+
struct TreeNode *right;
7+
};
8+
9+
bool isUnivalTree(struct TreeNode *root)
10+
{
11+
if(!root) return true;
12+
if(root->left) {
13+
if (root->left->val != root->val) return false;
14+
if (!isUnivalTree(root->left)) return false;
15+
}
16+
if(root->right) {
17+
if (root->right->val != root->val) return false;
18+
if (!isUnivalTree(root->right)) return false;
19+
}
20+
21+
return true;
22+
23+
}

src/solutions.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)