Skip to content

Commit ec93537

Browse files
author
Bogdan Hladiuc
committed
Day 14 Generate Password
1 parent 2ce58b8 commit ec93537

File tree

8 files changed

+156
-2
lines changed

8 files changed

+156
-2
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\..\..\Library\Library.csproj" />
5+
</ItemGroup>
6+
7+
<PropertyGroup>
8+
<TargetFramework>net6.0</TargetFramework>
9+
<ImplicitUsings>enable</ImplicitUsings>
10+
<Nullable>enable</Nullable>
11+
</PropertyGroup>
12+
13+
</Project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Library;
2+
namespace Formation {
3+
4+
public class GeneratePassword {
5+
public static void Run() {
6+
var chars = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };
7+
var min = 3;
8+
var max = 5;
9+
var result = new List<string>();
10+
var stack = new Stack<char>();
11+
var freqMap = new Dictionary<char, int>();
12+
13+
Solution(chars, min, max, result, stack, freqMap);
14+
15+
AssortedMethods.PrintStringArray(result.ToArray());
16+
}
17+
18+
private static void Solution(char[] chars, int min, int max, List<string> result, Stack<char> stack, Dictionary<char, int> freqMap) {
19+
if (stack.Count >= min && stack.Count <= max) {
20+
var password = string.Join("", stack.Reverse());
21+
result.Add(password);
22+
}
23+
24+
if (stack.Count == max) {
25+
return;
26+
}
27+
28+
foreach (char c in chars) {
29+
var count = freqMap.GetValueOrDefault(c, 0);
30+
31+
if ((stack.Count == 0 || stack.Peek() != c) && count < 2) {
32+
stack.Push(c);
33+
freqMap[c] = count + 1;
34+
35+
Solution(chars, min, max, result, stack, freqMap);
36+
37+
stack.Pop();
38+
freqMap[c]--;
39+
}
40+
}
41+
}
42+
}
43+
}

LeetcodeNew.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_12_Flatten_Sublist", "F
111111
EndProject
112112
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_13_Maze_Solver", "Formation\21_Days_Challenge\Day_13_Maze_Solver\Day_13_Maze_Solver.csproj", "{85892726-1503-4BDD-A04B-E4DCD172CFE0}"
113113
EndProject
114+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_14_GeneratePassword", "Formation\21_Days_Challenge\Day_14_GeneratePassword\Day_14_GeneratePassword.csproj", "{D26F74F9-F5FA-47B9-AB82-DD5EFD2AD1FF}"
115+
EndProject
114116
Global
115117
GlobalSection(SolutionConfigurationPlatforms) = preSolution
116118
Debug|Any CPU = Debug|Any CPU
@@ -748,6 +750,18 @@ Global
748750
{85892726-1503-4BDD-A04B-E4DCD172CFE0}.Release|x64.Build.0 = Release|Any CPU
749751
{85892726-1503-4BDD-A04B-E4DCD172CFE0}.Release|x86.ActiveCfg = Release|Any CPU
750752
{85892726-1503-4BDD-A04B-E4DCD172CFE0}.Release|x86.Build.0 = Release|Any CPU
753+
{D26F74F9-F5FA-47B9-AB82-DD5EFD2AD1FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
754+
{D26F74F9-F5FA-47B9-AB82-DD5EFD2AD1FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
755+
{D26F74F9-F5FA-47B9-AB82-DD5EFD2AD1FF}.Debug|x64.ActiveCfg = Debug|Any CPU
756+
{D26F74F9-F5FA-47B9-AB82-DD5EFD2AD1FF}.Debug|x64.Build.0 = Debug|Any CPU
757+
{D26F74F9-F5FA-47B9-AB82-DD5EFD2AD1FF}.Debug|x86.ActiveCfg = Debug|Any CPU
758+
{D26F74F9-F5FA-47B9-AB82-DD5EFD2AD1FF}.Debug|x86.Build.0 = Debug|Any CPU
759+
{D26F74F9-F5FA-47B9-AB82-DD5EFD2AD1FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
760+
{D26F74F9-F5FA-47B9-AB82-DD5EFD2AD1FF}.Release|Any CPU.Build.0 = Release|Any CPU
761+
{D26F74F9-F5FA-47B9-AB82-DD5EFD2AD1FF}.Release|x64.ActiveCfg = Release|Any CPU
762+
{D26F74F9-F5FA-47B9-AB82-DD5EFD2AD1FF}.Release|x64.Build.0 = Release|Any CPU
763+
{D26F74F9-F5FA-47B9-AB82-DD5EFD2AD1FF}.Release|x86.ActiveCfg = Release|Any CPU
764+
{D26F74F9-F5FA-47B9-AB82-DD5EFD2AD1FF}.Release|x86.Build.0 = Release|Any CPU
751765
EndGlobalSection
752766
GlobalSection(NestedProjects) = preSolution
753767
{45EB27A3-9FBB-48ED-8C89-E0F074C02E8C} = {71C7D88B-6D5D-4E25-A275-869603700831}
@@ -764,5 +778,6 @@ Global
764778
{D6602164-2EAF-4C6F-86DE-04ACAF8D0F02} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
765779
{B86F069E-A27D-48F5-ABD3-D2DA5253801E} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
766780
{85892726-1503-4BDD-A04B-E4DCD172CFE0} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
781+
{D26F74F9-F5FA-47B9-AB82-DD5EFD2AD1FF} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
767782
EndGlobalSection
768783
EndGlobal

Run/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ static void Main(string[] args) {
1616
// Formation.MergeSort.Run(); // Day 10
1717
// Formation.BinaryTreeHeight.Run(); // Day 11
1818
// Formation.FLattenSublist.Run(); // Day 12
19-
Formation.MazeSolver.Run(); // Day 13
19+
// Formation.MazeSolver.Run(); // Day 13
20+
Formation.GeneratePassword.Run(); // Day 14
2021

2122
#endregion Formation
2223

Run/Run.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_11_Height_Of_Binary_Tree_Q104\Day_11_Height_Of_Binary_Tree_Q104.csproj" />
5454
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_12_Flatten_Sublist\Day_12_Flatten_Sublist.csproj" />
5555
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_13_Maze_Solver\Day_13_Maze_Solver.csproj" />
56+
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_14_GeneratePassword\Day_14_GeneratePassword.csproj" />
5657
</ItemGroup>
5758

5859
<PropertyGroup>

Run/obj/Run.csproj.nuget.dgspec.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,62 @@
284284
}
285285
}
286286
},
287+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_14_GeneratePassword/Day_14_GeneratePassword.csproj": {
288+
"version": "1.0.0",
289+
"restore": {
290+
"projectUniqueName": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_14_GeneratePassword/Day_14_GeneratePassword.csproj",
291+
"projectName": "Day_14_GeneratePassword",
292+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_14_GeneratePassword/Day_14_GeneratePassword.csproj",
293+
"packagesPath": "/Users/Bogdan/.nuget/packages/",
294+
"outputPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_14_GeneratePassword/obj/",
295+
"projectStyle": "PackageReference",
296+
"configFilePaths": [
297+
"/Users/Bogdan/.nuget/NuGet/NuGet.Config"
298+
],
299+
"originalTargetFrameworks": [
300+
"net6.0"
301+
],
302+
"sources": {
303+
"https://api.nuget.org/v3/index.json": {}
304+
},
305+
"frameworks": {
306+
"net6.0": {
307+
"targetAlias": "net6.0",
308+
"projectReferences": {
309+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj": {
310+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj"
311+
}
312+
}
313+
}
314+
},
315+
"warningProperties": {
316+
"warnAsError": [
317+
"NU1605"
318+
]
319+
}
320+
},
321+
"frameworks": {
322+
"net6.0": {
323+
"targetAlias": "net6.0",
324+
"imports": [
325+
"net461",
326+
"net462",
327+
"net47",
328+
"net471",
329+
"net472",
330+
"net48"
331+
],
332+
"assetTargetFallback": true,
333+
"warn": true,
334+
"frameworkReferences": {
335+
"Microsoft.NETCore.App": {
336+
"privateAssets": "all"
337+
}
338+
},
339+
"runtimeIdentifierGraphPath": "/usr/local/share/dotnet/sdk/6.0.202/RuntimeIdentifierGraph.json"
340+
}
341+
}
342+
},
287343
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_1_Remove_Delete_Middle_Node_Q2095/Day_1_Remove_Delete_Middle_Node_Q2095.csproj": {
288344
"version": "1.0.0",
289345
"restore": {
@@ -2885,6 +2941,9 @@
28852941
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_13_Maze_Solver/Day_13_Maze_Solver.csproj": {
28862942
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_13_Maze_Solver/Day_13_Maze_Solver.csproj"
28872943
},
2944+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_14_GeneratePassword/Day_14_GeneratePassword.csproj": {
2945+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_14_GeneratePassword/Day_14_GeneratePassword.csproj"
2946+
},
28882947
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_1_Remove_Delete_Middle_Node_Q2095/Day_1_Remove_Delete_Middle_Node_Q2095.csproj": {
28892948
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_1_Remove_Delete_Middle_Node_Q2095/Day_1_Remove_Delete_Middle_Node_Q2095.csproj"
28902949
},

Run/obj/project.assets.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@
6767
"bin/placeholder/Day_13_Maze_Solver.dll": {}
6868
}
6969
},
70+
"Day_14_GeneratePassword/1.0.0": {
71+
"type": "project",
72+
"framework": ".NETCoreApp,Version=v6.0",
73+
"dependencies": {
74+
"Library": "1.0.0"
75+
},
76+
"compile": {
77+
"bin/placeholder/Day_14_GeneratePassword.dll": {}
78+
},
79+
"runtime": {
80+
"bin/placeholder/Day_14_GeneratePassword.dll": {}
81+
}
82+
},
7083
"Day_1_Remove_Delete_Middle_Node_Q2095/1.0.0": {
7184
"type": "project",
7285
"framework": ".NETCoreApp,Version=v6.0",
@@ -655,6 +668,11 @@
655668
"path": "../Formation/21_Days_Challenge/Day_13_Maze_Solver/Day_13_Maze_Solver.csproj",
656669
"msbuildProject": "../Formation/21_Days_Challenge/Day_13_Maze_Solver/Day_13_Maze_Solver.csproj"
657670
},
671+
"Day_14_GeneratePassword/1.0.0": {
672+
"type": "project",
673+
"path": "../Formation/21_Days_Challenge/Day_14_GeneratePassword/Day_14_GeneratePassword.csproj",
674+
"msbuildProject": "../Formation/21_Days_Challenge/Day_14_GeneratePassword/Day_14_GeneratePassword.csproj"
675+
},
658676
"Day_1_Remove_Delete_Middle_Node_Q2095/1.0.0": {
659677
"type": "project",
660678
"path": "../Formation/21_Days_Challenge/Day_1_Remove_Delete_Middle_Node_Q2095/Day_1_Remove_Delete_Middle_Node_Q2095.csproj",
@@ -898,6 +916,7 @@
898916
"Day_11_Height_Of_Binary_Tree_Q104 >= 1.0.0",
899917
"Day_12_Flatten_Sublist >= 1.0.0",
900918
"Day_13_Maze_Solver >= 1.0.0",
919+
"Day_14_GeneratePassword >= 1.0.0",
901920
"Day_1_Remove_Delete_Middle_Node_Q2095 >= 1.0.0",
902921
"Day_2_Spiral_Traversal_Q54 >= 1.0.0",
903922
"Day_3_Reverse_Groups_of_K_Nodes_Q25 >= 1.0.0",
@@ -987,6 +1006,9 @@
9871006
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_13_Maze_Solver/Day_13_Maze_Solver.csproj": {
9881007
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_13_Maze_Solver/Day_13_Maze_Solver.csproj"
9891008
},
1009+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_14_GeneratePassword/Day_14_GeneratePassword.csproj": {
1010+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_14_GeneratePassword/Day_14_GeneratePassword.csproj"
1011+
},
9901012
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_1_Remove_Delete_Middle_Node_Q2095/Day_1_Remove_Delete_Middle_Node_Q2095.csproj": {
9911013
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_1_Remove_Delete_Middle_Node_Q2095/Day_1_Remove_Delete_Middle_Node_Q2095.csproj"
9921014
},

Run/obj/project.nuget.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"version": 2,
3-
"dgSpecHash": "OSjFbRYQV9VJOWMkYoxr5Bcl77DRlCNCHbteaOBT/6+1fKA9sn0DnNflrbMLSUwzgWYCZDaoetcWYhYoc+tOow==",
3+
"dgSpecHash": "sEZVhMi9c0+e06qc8+FTSQ/cVLwABkBLe/qy0G1AJvtEVi9shV8jg2FXruFWUkWHuRfhzhITKFNZmP2v5i1HUg==",
44
"success": true,
55
"projectFilePath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Run/Run.csproj",
66
"expectedPackageFiles": [],

0 commit comments

Comments
 (0)