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

+1
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": "<div><p>You are given an array of strings&nbsp;<code>words</code>&nbsp;and a string&nbsp;<code>chars</code>.</p>\n\n<p>A string is <em>good</em>&nbsp;if&nbsp;it can be formed by&nbsp;characters from <code>chars</code>&nbsp;(each character&nbsp;can only be used once).</p>\n\n<p>Return the sum of lengths of all good strings in <code>words</code>.</p>\n\n<p>&nbsp;</p>\n\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input: </strong>words = <span id=\"example-input-1-1\">[\"cat\",\"bt\",\"hat\",\"tree\"]</span>, chars = <span id=\"example-input-1-2\">\"atach\"</span>\n<strong>Output: </strong><span id=\"example-output-1\">6</span>\n<strong>Explanation: </strong>\nThe strings that can be formed are \"cat\" and \"hat\" so the answer is 3 + 3 = 6.\n</pre>\n\n<p><strong>Example 2:</strong></p>\n\n<pre><strong>Input: </strong>words = <span id=\"example-input-2-1\">[\"hello\",\"world\",\"leetcode\"]</span>, chars = <span id=\"example-input-2-2\">\"welldonehoneyr\"</span>\n<strong>Output: </strong><span id=\"example-output-2\">10</span>\n<strong>Explanation: </strong>\nThe strings that can be formed are \"hello\" and \"world\" so the answer is 5 + 5 = 10.\n</pre>\n\n<p>&nbsp;</p>\n\n<p><span><strong>Note:</strong></span></p>\n\n<ol>\n\t<li><code>1 &lt;= words.length &lt;= 1000</code></li>\n\t<li><code>1 &lt;= words[i].length, chars.length&nbsp;&lt;= 100</code></li>\n\t<li>All strings contain lowercase English letters only.</li>\n</ol></div>", "language": "cpp", "solution": "#include <iostream>\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution\n{\npublic:\n int countCharacters(vector<string> &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

+1
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": "<div><p>The&nbsp;<b>Fibonacci numbers</b>, commonly denoted&nbsp;<code>F(n)</code>&nbsp;form a sequence, called the&nbsp;<b>Fibonacci sequence</b>, such that each number is the sum of the two preceding ones, starting from <code>0</code> and <code>1</code>. That is,</p>\n\n<pre>F(0) = 0,&nbsp; &nbsp;F(1)&nbsp;= 1\nF(N) = F(N - 1) + F(N - 2), for N &gt; 1.\n</pre>\n\n<p>Given <code>N</code>, calculate <code>F(N)</code>.</p>\n\n<p>&nbsp;</p>\n\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input:</strong> 2\n<strong>Output:</strong> 1\n<strong>Explanation:</strong> F(2) = F(1) + F(0) = 1 + 0 = 1.\n</pre>\n\n<p><strong>Example 2:</strong></p>\n\n<pre><strong>Input:</strong> 3\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> F(3) = F(2) + F(1) = 1 + 1 = 2.\n</pre>\n\n<p><strong>Example 3:</strong></p>\n\n<pre><strong>Input:</strong> 4\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> F(4) = F(3) + F(2) = 2 + 1 = 3.\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>Note:</strong></p>\n\n<p>0 \u2264 <code>N</code> \u2264 30.</p>\n</div>", "language": "cpp", "solution": "#include <iostream>\n#include <bits/stdc++.h>\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

+1
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": "<div><p>Given a string <code>S</code>&nbsp;and a character <code>C</code>, return an array of integers representing the shortest distance from the character <code>C</code> in the string.</p>\n\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input:</strong> S = \"loveleetcode\", C = 'e'\n<strong>Output:</strong> [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>Note:</strong></p>\n\n<ol>\n\t<li><code>S</code> string length is&nbsp;in&nbsp;<code>[1, 10000].</code></li>\n\t<li><code>C</code>&nbsp;is a single character, and guaranteed to be in string <code>S</code>.</li>\n\t<li>All letters in <code>S</code> and <code>C</code> are lowercase.</li>\n</ol>\n</div>", "language": "cpp", "solution": "#include <iostream>\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution\n{\npublic:\n vector<int> shortestToChar(string S, char C)\n {\n vector<int> 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<int> 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

+1
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": "<div><p>Every email consists of a local name and a domain name, separated by the @ sign.</p>\n\n<p>For example, in <code>alice@leetcode.com</code>,&nbsp;<code>alice</code> is the local name, and <code>leetcode.com</code> is the domain name.</p>\n\n<p>Besides lowercase letters, these emails may contain <code>'.'</code>s or <code>'+'</code>s.</p>\n\n<p>If you add periods (<code>'.'</code>) between some characters in the <strong>local name</strong> part of an email address, mail sent there will be forwarded to the same address without dots in the local name.&nbsp; For example, <code>\"alice.z@leetcode.com\"</code> and <code>\"alicez@leetcode.com\"</code> forward to the same email address.&nbsp; (Note that this rule does not apply for domain names.)</p>\n\n<p>If you add a plus (<code>'+'</code>) in the <strong>local name</strong>, everything after the first plus sign will be&nbsp;<strong>ignored</strong>. This allows certain emails to be filtered, for example&nbsp;<code>m.y+name@email.com</code>&nbsp;will be forwarded to&nbsp;<code>my@email.com</code>.&nbsp; (Again, this rule does not apply for domain names.)</p>\n\n<p>It is possible to use both of these rules at the same time.</p>\n\n<p>Given a list of <code>emails</code>, we send one email to each address in the list.&nbsp;&nbsp;How many different addresses actually receive mails?&nbsp;</p>\n\n<p>&nbsp;</p>\n\n<div>\n<p><strong>Example 1:</strong></p>\n\n<pre><strong>Input: </strong><span id=\"example-input-1-1\">[\"test.email+alex@leetcode.com\",\"test.e.mail+bob.cathy@leetcode.com\",\"testemail+david@lee.tcode.com\"]</span>\n<strong>Output: </strong><span id=\"example-output-1\">2</span>\n<strong><span>Explanation:</span></strong><span>&nbsp;\"</span><span id=\"example-input-1-1\">testemail@leetcode.com\" and \"testemail@lee.tcode.com\" </span>actually receive mails\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>Note:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= emails[i].length&nbsp;&lt;= 100</code></li>\n\t<li><code>1 &lt;= emails.length &lt;= 100</code></li>\n\t<li>Each <code>emails[i]</code> contains exactly one <code>'@'</code> character.</li>\n\t<li>All local and domain names are non-empty.</li>\n\t<li>Local names do not start with a <code>'+'</code> character.</li>\n</ul>\n</div>\n</div>", "language": "cpp", "solution": "#include <iostream>\n#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution\n{\npublic:\n int numUniqueEmails(vector<string> &emails)\n {\n\n set<string> 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

+1
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": "<div><p>A binary tree is <em>univalued</em> if every node in the tree has the same value.</p>\n\n<p>Return <code>true</code>&nbsp;if and only if the given tree is univalued.</p>\n\n<p>&nbsp;</p>\n\n<p><strong>Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2018/12/28/unival_bst_1.png\" style=\"width: 265px; height: 172px;\">\n<pre><strong>Input: </strong><span id=\"example-input-1-1\">[1,1,1,1,1,null,1]</span>\n<strong>Output: </strong><span id=\"example-output-1\">true</span>\n</pre>\n\n<div>\n<p><strong>Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2018/12/28/unival_bst_2.png\" style=\"width: 198px; height: 169px;\">\n<pre><strong>Input: </strong><span id=\"example-input-2-1\">[2,2,2,5,2]</span>\n<strong>Output: </strong><span id=\"example-output-2\">false</span>\n</pre>\n</div>\n\n<p>&nbsp;</p>\n\n<p><strong>Note:</strong></p>\n\n<ol>\n\t<li>The number of nodes in the given tree will be in the range <code>[1, 100]</code>.</li>\n\t<li>Each node's value will be an integer in the range <code>[0, 99]</code>.</li>\n</ol>\n</div>", "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

+44
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<string> &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

+26
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

+46
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

+31
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<string> &emails)
10+
{
11+
12+
set<string> 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

+23
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

+1-1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)