Created
April 10, 2017 00:48
-
-
Save jianminchen/888ba174384e93e9ac3e1a76f3050620 to your computer and use it in GitHub Desktop.
Slide window - string search - 30 minutes performance - a few of bugs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
/// | |
/// line scan | |
// brute force, O(n^2), any substring, find something | |
// I cannot hear you | |
// optimal | |
// start, end = 0 -> scan the array | |
// end-> move, include all chars, find one | |
// design scheme to move left point -> | |
// x, y, z -> xyxx, this way, you can move left point to next one, first x | |
// yxxz -> all x, y, z are included, so I record length = 4 | |
// yxxzy-> move left, remove y, check x, remove x, xzy => compared to existing length, min = 3, beat 3, stop | |
// xzy | |
// record count of x, y, z, this way, if I can move left point or not <- good design <- slide window | |
// only scan array once -> O(n), sliding windows -> dictionary <char, int> , add/min, avoid scan the window extra | |
// cannot hear you ...What is your question | |
// cannot hear you again | |
// edge case -> unique array, should not empty, alphabeat, maximum 26, | |
// | |
class PracticeSlideWindow { | |
static void Main(string[] args) { | |
Console.WriteLine("Practice Slide Window"); | |
} | |
/// linear scan the array, keep two pointers, left/ right, sliding window, keep the slide window count of unqiure chars | |
// record minimum string, later just get the length | |
public static string FindSmallestSubstringContainingUniqueChars_LinearScan(string s, string unique) | |
{ | |
if(unique == null || unique.Length == 0) return string.Empty; | |
// slide winodow | |
int length = s.Length; | |
var bookKeeping = new Dictionary<char, int>(); | |
// | |
bool[] uniqueAlphbetic = new bool[26]; // ? | |
int minimumLength = int32.Maxvalue; | |
string mimumumStr = string.empty; | |
int left = 0; | |
int right = 0; | |
for(int i = 0; i < length; i++) | |
{ | |
char current = s[i]; | |
if(!bookKeeping.ContainsKey(current)) // x | |
{ | |
end++; // end = i | |
bookKeeping.Add(current, 1); | |
// need to check if the string has everything | |
if(slidewindowContainsAllKey(bookKeeping, unique)) | |
{ | |
int currentLength = end - start; | |
if(minimumLegnth > currentLength) | |
{ | |
minimumLength = currentLength; | |
mimimumStr = s.substring(start, end - start); | |
// reset start, end two pointers | |
start = i; | |
end = i; | |
} | |
//determine if I need to move left side point | |
// left or not ? | |
} | |
} | |
else{ | |
// check if the left point is the same char, move left pointer | |
char leftC = s[left]; | |
if(leftC == current){ | |
start ++; | |
} | |
else | |
{ | |
bookKeep[current]++; | |
} | |
// otherwsie, I need to add the count of char | |
} | |
} | |
return minimumStr; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment