I want to create the following language:
L={a^nb^m| n+m=k ,n,m>=0}
for a constant k.
I am using a Regexnamespace class System.Text.RegularExpressions.
The best solution I have now is:
public void Match(string input, int k)
{
Regex regex = new Regex(@"a*b*");
Match match = regex.Match(input);
if(match.Length==k)
Console.WriteLine("Successfully");
else
Console.WriteLine("Don't match");
}
For k=5successfully used the following inputs:
"aaabb"
"aabbb"
"aaaaa"
This, for example, is not:
"aaabbb"
"ab"
What is the most elegant way to achieve this?
source
share