Skip to content

Commit 4c161a1

Browse files
author
Bogdan Hladiuc
committed
Q20 Valid Parentheses
1 parent 577317f commit 4c161a1

File tree

8 files changed

+190
-2
lines changed

8 files changed

+190
-2
lines changed

LeetcodeNew.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Q387_First_Unique_Character
7777
EndProject
7878
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Q_Missing_Plus_Two", "Q_Missing_Plus_Two\Q_Missing_Plus_Two.csproj", "{E9A1A414-3117-41D9-990E-ABA5D910B19C}"
7979
EndProject
80+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Q20_Valid_Parentheses", "Q20_Valid_Parentheses\Q20_Valid_Parentheses.csproj", "{9520CEAE-DA4F-4249-A741-9F1F487B5CE9}"
81+
EndProject
8082
Global
8183
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8284
Debug|Any CPU = Debug|Any CPU
@@ -534,5 +536,17 @@ Global
534536
{E9A1A414-3117-41D9-990E-ABA5D910B19C}.Release|x64.Build.0 = Release|Any CPU
535537
{E9A1A414-3117-41D9-990E-ABA5D910B19C}.Release|x86.ActiveCfg = Release|Any CPU
536538
{E9A1A414-3117-41D9-990E-ABA5D910B19C}.Release|x86.Build.0 = Release|Any CPU
539+
{9520CEAE-DA4F-4249-A741-9F1F487B5CE9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
540+
{9520CEAE-DA4F-4249-A741-9F1F487B5CE9}.Debug|Any CPU.Build.0 = Debug|Any CPU
541+
{9520CEAE-DA4F-4249-A741-9F1F487B5CE9}.Debug|x64.ActiveCfg = Debug|Any CPU
542+
{9520CEAE-DA4F-4249-A741-9F1F487B5CE9}.Debug|x64.Build.0 = Debug|Any CPU
543+
{9520CEAE-DA4F-4249-A741-9F1F487B5CE9}.Debug|x86.ActiveCfg = Debug|Any CPU
544+
{9520CEAE-DA4F-4249-A741-9F1F487B5CE9}.Debug|x86.Build.0 = Debug|Any CPU
545+
{9520CEAE-DA4F-4249-A741-9F1F487B5CE9}.Release|Any CPU.ActiveCfg = Release|Any CPU
546+
{9520CEAE-DA4F-4249-A741-9F1F487B5CE9}.Release|Any CPU.Build.0 = Release|Any CPU
547+
{9520CEAE-DA4F-4249-A741-9F1F487B5CE9}.Release|x64.ActiveCfg = Release|Any CPU
548+
{9520CEAE-DA4F-4249-A741-9F1F487B5CE9}.Release|x64.Build.0 = Release|Any CPU
549+
{9520CEAE-DA4F-4249-A741-9F1F487B5CE9}.Release|x86.ActiveCfg = Release|Any CPU
550+
{9520CEAE-DA4F-4249-A741-9F1F487B5CE9}.Release|x86.Build.0 = Release|Any CPU
537551
EndGlobalSection
538552
EndGlobal
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#nullable disable warnings
2+
using System.Diagnostics;
3+
4+
namespace Q20_Valid_Parentheses {
5+
6+
public class Q20_Valid_Parentheses {
7+
public static void Run() {
8+
var tests = new List<Test> {
9+
new Test {
10+
Id = 1,
11+
Text = "[()()]",
12+
Expected = true
13+
},
14+
new Test {
15+
Id = 2,
16+
Text = "[(]()]",
17+
Expected = false,
18+
},
19+
new Test {
20+
Id = 3,
21+
Text = "[{()}]",
22+
Expected = true
23+
},
24+
new Test {
25+
Id = 4,
26+
Text = "((()))",
27+
Expected = true
28+
},
29+
new Test {
30+
Id = 5,
31+
Text = "[(])",
32+
Expected = false
33+
},
34+
new Test {
35+
Id = 6,
36+
Text = "((()))",
37+
Expected = true
38+
},
39+
new Test {
40+
Id = 7,
41+
Text = "()]]",
42+
Expected = false
43+
},
44+
new Test {
45+
Id = 8,
46+
Text = "()[]{}()[]{}",
47+
Expected = true
48+
}
49+
};
50+
51+
foreach (var test in tests) {
52+
var result = Solution(test.Text);
53+
test.Verify(result);
54+
}
55+
}
56+
57+
private static bool Solution(string text) {
58+
var parenthesesMap = new Dictionary<char, char> {
59+
{ ')', '(' },
60+
{ ']', '[' },
61+
{ '}', '{' }
62+
};
63+
var stack = new Stack<char>();
64+
var openParentheses = new HashSet<char> { '(', '[', '{' };
65+
66+
foreach (var c in text) {
67+
if (openParentheses.Contains(c)) {
68+
stack.Push(c);
69+
} else {
70+
if (stack.Count == 0 || !stack.Pop().Equals(parenthesesMap[c])) {
71+
return false;
72+
}
73+
}
74+
}
75+
76+
return stack.Count == 0;
77+
}
78+
}
79+
80+
internal class Test {
81+
public int Id { get; set; }
82+
public string Text { get; set; }
83+
public bool Expected { get; set; }
84+
85+
public void Verify(bool result) {
86+
Debug.Assert(result == Expected, $"Test {Id} failed.");
87+
}
88+
}
89+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

Run/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
namespace Run {
22
class Program {
33
static void Main(string[] args) {
4-
Q_Missing_Plus_Two.Q_Missing_Plus_Two.Run();
4+
//Q_Missing_Plus_Two.Q_Missing_Plus_Two.Run();
55

66
// Q10_Regular_Expression_Matching.Q10_Regular_Expression_Matching.Run();
7+
Q20_Valid_Parentheses.Q20_Valid_Parentheses.Run();
78
// Q23_Merge_K_Sorted_Lists.Q23_Merge_K_Sorted_Lists.Run();
89
// Q29_Divide_Two_Integers.Q29_Divide_Two_Integers.Run();
910
// Q56_Merge_Intervals.Q56_Merge_Intervals.Run();

Run/Run.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<ProjectReference Include="..\Q189_Rotate_Array\Q189_Rotate_Array.csproj" />
3939
<ProjectReference Include="..\Q387_First_Unique_Character_In_a_String\Q387_First_Unique_Character_In_a_String.csproj" />
4040
<ProjectReference Include="..\Q_Missing_Plus_Two\Q_Missing_Plus_Two.csproj" />
41+
<ProjectReference Include="..\Q20_Valid_Parentheses\Q20_Valid_Parentheses.csproj" />
4142
</ItemGroup>
4243

4344
<PropertyGroup>

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,58 @@
600600
}
601601
}
602602
},
603+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q20_Valid_Parentheses/Q20_Valid_Parentheses.csproj": {
604+
"version": "1.0.0",
605+
"restore": {
606+
"projectUniqueName": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q20_Valid_Parentheses/Q20_Valid_Parentheses.csproj",
607+
"projectName": "Q20_Valid_Parentheses",
608+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q20_Valid_Parentheses/Q20_Valid_Parentheses.csproj",
609+
"packagesPath": "/Users/Bogdan/.nuget/packages/",
610+
"outputPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q20_Valid_Parentheses/obj/",
611+
"projectStyle": "PackageReference",
612+
"configFilePaths": [
613+
"/Users/Bogdan/.nuget/NuGet/NuGet.Config"
614+
],
615+
"originalTargetFrameworks": [
616+
"net6.0"
617+
],
618+
"sources": {
619+
"https://api.nuget.org/v3/index.json": {}
620+
},
621+
"frameworks": {
622+
"net6.0": {
623+
"targetAlias": "net6.0",
624+
"projectReferences": {}
625+
}
626+
},
627+
"warningProperties": {
628+
"warnAsError": [
629+
"NU1605"
630+
]
631+
}
632+
},
633+
"frameworks": {
634+
"net6.0": {
635+
"targetAlias": "net6.0",
636+
"imports": [
637+
"net461",
638+
"net462",
639+
"net47",
640+
"net471",
641+
"net472",
642+
"net48"
643+
],
644+
"assetTargetFallback": true,
645+
"warn": true,
646+
"frameworkReferences": {
647+
"Microsoft.NETCore.App": {
648+
"privateAssets": "all"
649+
}
650+
},
651+
"runtimeIdentifierGraphPath": "/usr/local/share/dotnet/sdk/6.0.202/RuntimeIdentifierGraph.json"
652+
}
653+
}
654+
},
603655
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q211_Design_Add_Search_Words_Data_Structure/Q211_Design_Add_Search_Words_Data_Structure.csproj": {
604656
"version": "1.0.0",
605657
"restore": {
@@ -2071,6 +2123,9 @@
20712123
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q199_Binary_Tree_Right_Side_View/Q199_Binary_Tree_Right_Side_View.csproj": {
20722124
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q199_Binary_Tree_Right_Side_View/Q199_Binary_Tree_Right_Side_View.csproj"
20732125
},
2126+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q20_Valid_Parentheses/Q20_Valid_Parentheses.csproj": {
2127+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q20_Valid_Parentheses/Q20_Valid_Parentheses.csproj"
2128+
},
20742129
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q211_Design_Add_Search_Words_Data_Structure/Q211_Design_Add_Search_Words_Data_Structure.csproj": {
20752130
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q211_Design_Add_Search_Words_Data_Structure/Q211_Design_Add_Search_Words_Data_Structure.csproj"
20762131
},

Run/obj/project.assets.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@
130130
"bin/placeholder/Q199_Binary_Tree_Right_Side_View.dll": {}
131131
}
132132
},
133+
"Q20_Valid_Parentheses/1.0.0": {
134+
"type": "project",
135+
"framework": ".NETCoreApp,Version=v6.0",
136+
"compile": {
137+
"bin/placeholder/Q20_Valid_Parentheses.dll": {}
138+
},
139+
"runtime": {
140+
"bin/placeholder/Q20_Valid_Parentheses.dll": {}
141+
}
142+
},
133143
"Q211_Design_Add_Search_Words_Data_Structure/1.0.0": {
134144
"type": "project",
135145
"framework": ".NETCoreApp,Version=v6.0",
@@ -496,6 +506,11 @@
496506
"path": "../Q199_Binary_Tree_Right_Side_View/Q199_Binary_Tree_Right_Side_View.csproj",
497507
"msbuildProject": "../Q199_Binary_Tree_Right_Side_View/Q199_Binary_Tree_Right_Side_View.csproj"
498508
},
509+
"Q20_Valid_Parentheses/1.0.0": {
510+
"type": "project",
511+
"path": "../Q20_Valid_Parentheses/Q20_Valid_Parentheses.csproj",
512+
"msbuildProject": "../Q20_Valid_Parentheses/Q20_Valid_Parentheses.csproj"
513+
},
499514
"Q211_Design_Add_Search_Words_Data_Structure/1.0.0": {
500515
"type": "project",
501516
"path": "../Q211_Design_Add_Search_Words_Data_Structure/Q211_Design_Add_Search_Words_Data_Structure.csproj",
@@ -640,6 +655,7 @@
640655
"Q1792_Buildings_With_Ocean_View >= 1.0.0",
641656
"Q189_Rotate_Array >= 1.0.0",
642657
"Q199_Binary_Tree_Right_Side_View >= 1.0.0",
658+
"Q20_Valid_Parentheses >= 1.0.0",
643659
"Q211_Design_Add_Search_Words_Data_Structure >= 1.0.0",
644660
"Q215_Kth_Largest_Element >= 1.0.0",
645661
"Q236_Lowest_Common_Ancestor_of_a_Binary_Tree >= 1.0.0",
@@ -726,6 +742,9 @@
726742
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q199_Binary_Tree_Right_Side_View/Q199_Binary_Tree_Right_Side_View.csproj": {
727743
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q199_Binary_Tree_Right_Side_View/Q199_Binary_Tree_Right_Side_View.csproj"
728744
},
745+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q20_Valid_Parentheses/Q20_Valid_Parentheses.csproj": {
746+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q20_Valid_Parentheses/Q20_Valid_Parentheses.csproj"
747+
},
729748
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q211_Design_Add_Search_Words_Data_Structure/Q211_Design_Add_Search_Words_Data_Structure.csproj": {
730749
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q211_Design_Add_Search_Words_Data_Structure/Q211_Design_Add_Search_Words_Data_Structure.csproj"
731750
},

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": "KycI5MnNcIyTqGUw3BR7dywmtw3tvigXcCbFZzBnW0o3lc6shU6dJXOAxly3aMe+0OizAi/3Af5KJTywDehxoQ==",
3+
"dgSpecHash": "mTvvr0YqL/EROUJGtVl0WyypKnSP7ayTFzJMGSc/vXMT3toF8x53jvAJP9d3j7ziIE6XjofXdhSTr9Rzeuc+wg==",
44
"success": true,
55
"projectFilePath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Run/Run.csproj",
66
"expectedPackageFiles": [],

0 commit comments

Comments
 (0)