using System.Text.RegularExpressions;
using System.Collections.Generic;
static char[] alphabet = { 'a', 'b', 'c' };
static List<string> GenerateWords(int n)
List<string> words = new();
int alphabetSize = alphabet.Length;
int[] indices = new int[n];
for (int i = 0; i < n; i++)
int remainder = index % alphabetSize;
word = alphabet[remainder] + word;
word = alphabet[0] + word;
static void FindRegex(List<string> words, string finalChars)
for (int i = 0; i <alphabet.Length; i++)
if (i != alphabet.Length - 1) regex += '|';
regex += ")+(" + finalChars+")$";
Console.WriteLine("Regular expression: " + regex);
List<string> matchingWords = new List<string>();
foreach (string word in words)
if (Regex.IsMatch(word, regex))
if (matchingWords.Count == 0) Console.WriteLine("Words not found or final symbols are not contained in this alphabet");
Console.WriteLine("Matching words: ");
foreach (string word in matchingWords)
static void Main(string[] args)
List<string> words= GenerateWords(30);