Skip to content

Instantly share code, notes, and snippets.

@mshwf
Created June 23, 2019 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mshwf/098d0a86c7f68a0b7904bf6dbaf7420e to your computer and use it in GitHub Desktop.
Save mshwf/098d0a86c7f68a0b7904bf6dbaf7420e to your computer and use it in GitHub Desktop.
finds occurrences of a sub-string in a string
List<Kmer> kmers = new List<Kmer>();
string message = "gjhcebfu71uevkkvhjv,yjvykku71jrbirbgerngtgntteu71agg6g79htu718eay8hibioay+8eru71ohob8vy89egyhoih89vu71oyhbigr89yhrrvoaeoghau71e8ryg98ergh.oaeryl9g7rv7g7iytg643483gi@$#t32@#2R23r213ri4tyg2ti.88oichu71tp9hg";
for (int i = 0; i <= message.Length - 3; i++)
{
string substring = message.Substring(i, 3);
int occurences = Regex.Matches(message, Regex.Escape(substring)).Count;
kmers.Add(new Kmer { Text = substring, Occ = occurences });
}
foreach (var kmer in kmers.OrderByDescending(k => k.Occ).ThenByDescending(k => k.Text).Distinct(new KMerQualty()))
{
Console.WriteLine($"{kmer.Text}=>{kmer.Occ} times");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment