-
Notifications
You must be signed in to change notification settings - Fork 5
/
JewelsAndStones.cs
63 lines (59 loc) · 2.17 KB
/
JewelsAndStones.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Text;
namespace LeetCodeSolutionsLib
{
public class JewelsAndStones : Solution
{
/** Problem: Jewels and Stones
*
* You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each char in S is a type of stone you have. You want to know how many of the stones you have are also jewels.
*
* Example:
* Input: J = "abbC" S = "aAbBcC"
* Output : 2
* Explanation: string 'S' only contains chars 'a' and 'C' from J
*/
private string j;
private string s;
public JewelsAndStones(string J, string S)
{
j = J;
s = S;
}
private int NumJewlsInStones(string J, string S)
{
//Intent: We have 2 strings, J, S. We want to know how many times a char from J is found in S
//1) Loop through S.Length using i counter
//2) Loop through J.Length using j counter
//3) Loop through each char in the longer string, using a j counter
// if shorterString[i] == longerString[j]
// counter++
//4) Return counter || 0;
//Time Complexity: O(n^2)
//Space Complexity: 1
int counter = 0;
for (int i = 0; i < S.Length; i++)
{
for (int j = 0; j < J.Length; j++)
{
if (J[j] == S[i])
{
counter++;
}
}
}
return counter;
}
public override void PrintExample()
{
var watch = System.Diagnostics.Stopwatch.StartNew();
var results = NumJewlsInStones(this.j, this.s);
watch.Stop();
Console.WriteLine($"771. Jewels and Stones \n" +
$"Input String = J = '{j}' S = '{s}'\n" +
$"Result: [{results}] \n" +
$"Execution Speed: {watch.ElapsedMilliseconds}ms \n");
}
}
}