Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4c4530f

Browse files
ADD Minimum-Window-Substring problem #54
1 parent 8b70a46 commit 4c4530f

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

‎Hard/minimum-window-substring.cs

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
public class Solution
5+
{
6+
public static void Main(string[] args)
7+
{
8+
Console.WriteLine(MinWindow("ADOBECODEBANC", "ABC"));
9+
Console.WriteLine(MinWindow2("ADOBECODEBANC", "ABC"));
10+
11+
Console.ReadKey();
12+
}
13+
14+
#region Approach 1 (Brute Force)
15+
private static bool _IsSubstringIncludingText(string substring, string text)
16+
{
17+
if (string.IsNullOrWhiteSpace(substring) ||
18+
string.IsNullOrWhiteSpace(text) ||
19+
(substring.Length < text.Length))
20+
{
21+
return false;
22+
}
23+
24+
for (int i = 0; i < text.Length; i++)
25+
{
26+
if (!substring.Contains(text[i].ToString()))
27+
{
28+
return false;
29+
}
30+
31+
int indexToDelete = substring.IndexOf(text[i]);
32+
substring = substring.Remove(indexToDelete, 1);
33+
}
34+
35+
return true;
36+
}
37+
38+
public static string MinWindow(string s, string t)
39+
{
40+
if (string.IsNullOrWhiteSpace(s) || string.IsNullOrWhiteSpace(t) || (s.Length < t.Length))
41+
{
42+
return "";
43+
}
44+
45+
int startLengthOfWindow = 0;
46+
int minEndLengthOfWindow = t.Length;
47+
string windowSubstring = "";
48+
string result = s + t;
49+
bool takeNextLetter = true;
50+
51+
for (int i = startLengthOfWindow; i < s.Length; i++)
52+
{
53+
if (takeNextLetter)
54+
{
55+
windowSubstring += s[i];
56+
}
57+
58+
if (windowSubstring.Length >= minEndLengthOfWindow)
59+
{
60+
if (_IsSubstringIncludingText(windowSubstring, t))
61+
{
62+
result = (windowSubstring.Length < result.Length) ? windowSubstring : result;
63+
64+
if (result.Length == minEndLengthOfWindow)
65+
{
66+
return result;
67+
}
68+
69+
windowSubstring = windowSubstring.Substring(1);
70+
i--;
71+
takeNextLetter = false;
72+
}
73+
else
74+
{
75+
takeNextLetter = true;
76+
}
77+
}
78+
79+
}
80+
81+
return result == (s + t) ? "" : result;
82+
}
83+
#endregion
84+
85+
#region Approach 2
86+
public static string MinWindow2(string s, string t)
87+
{
88+
if (string.IsNullOrWhiteSpace(s) || string.IsNullOrWhiteSpace(t) || (s.Length < t.Length))
89+
{
90+
return "";
91+
}
92+
93+
var countT = InitializeDictionary(t);
94+
var window = new Dictionary<char, int>();
95+
int have = 0;
96+
int need = countT.Count;
97+
int left = 0;
98+
int[] result = new[] { -1, -1 };
99+
int minLength = int.MaxValue;
100+
101+
for (int right = 0; right < s.Length; right++)
102+
{
103+
char c = s[right];
104+
AddCharToDictionary(c, window);
105+
106+
if (countT.ContainsKey(c) && window[c] == countT[c])
107+
{
108+
have++;
109+
}
110+
111+
while (have == need)
112+
{
113+
int windowSize = right - left + 1;
114+
if (windowSize < minLength)
115+
{
116+
result = new[] { left, right };
117+
minLength = windowSize;
118+
}
119+
120+
AdjustWindow(s, left, window, countT, ref have);
121+
left++;
122+
}
123+
}
124+
125+
return minLength == int.MaxValue
126+
? string.Empty
127+
: s.Substring(result[0], result[1] - result[0] + 1);
128+
}
129+
130+
private static void AddCharToDictionary(char character, Dictionary<char, int> dict)
131+
{
132+
if (dict.ContainsKey(character))
133+
{
134+
dict[character]++;
135+
}
136+
else
137+
{
138+
dict.Add(character, 1);
139+
}
140+
}
141+
142+
private static Dictionary<char, int> InitializeDictionary(string t)
143+
{
144+
var countT = new Dictionary<char, int>();
145+
foreach (char c in t)
146+
{
147+
AddCharToDictionary(c, countT);
148+
}
149+
return countT;
150+
}
151+
152+
// Function to adjust the sliding window
153+
private static void AdjustWindow(string s, int left, Dictionary<char, int> window, Dictionary<char, int> countT, ref int have)
154+
{
155+
window[s[left]]--;
156+
if (countT.ContainsKey(s[left]) && window[s[left]] < countT[s[left]])
157+
{
158+
have--;
159+
}
160+
}
161+
#endregion
162+
}
163+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /