Skip to content

Instantly share code, notes, and snippets.

@WennderSantos
Created July 13, 2019 22:21
Show Gist options
  • Save WennderSantos/ea7923b8d88eb094e027944956c5e9b7 to your computer and use it in GitHub Desktop.
Save WennderSantos/ea7923b8d88eb094e027944956c5e9b7 to your computer and use it in GitHub Desktop.
Determine the maximum number of letters within "s" that are between two "programmer strings"
private static char[] CheckerHelper() => "programmer".ToCharArray();
//Time: O(n) where n is the length of s
//Memory: O(1) using a fixed length char[] checkerHelper
static int StringProgrammer(string s)
{
var checkerHelper = CheckerHelper();
var lettersFound = 0;
for(var i = 0; i < s.Length; i++)
{
var index = Array.IndexOf(checkerHelper, s[i]);
if (index > -1) //current letter exists in 'programmer' checkerHelper
{
checkerHelper[index] = '0'; //mark letter as found in checkerHelper
lettersFound++;
if (lettersFound == 10) //found first programmer
{
//when find the first programmer
//reset checkerHelper to its initial state
//reverse the ramaining chars of s
//reset i to initial its state.
//result will be the amount of chars ramaining after
//find the second programmer
checkerHelper = CheckerHelper();
var sTemp = s.Substring(i + 1).ToCharArray();
Array.Reverse(sTemp);
s = new string(sTemp);
i = -1;
}
else if (lettersFound == 20) //found second programmer
return s.Length - i - 1;
}
}
return -1;
}
static void Main(string[] args)
{
//StringProgrammer("xprogxrmaxemrppprmmograeiruu");
//2
//StringProgrammer("progxrammerrxproxgrammer");
//2
//Console.WriteLine(StringProgrammer("programmerlprogrammer"));
//1
//StringProgrammer("programmerprogrammer");
//0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment