Skip to content

Instantly share code, notes, and snippets.

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 jianminchen/56c254bc1f717fdd2e8979f187e99ce0 to your computer and use it in GitHub Desktop.
Save jianminchen/56c254bc1f717fdd2e8979f187e99ce0 to your computer and use it in GitHub Desktop.
Leetcode 10 - regular expression match - more code review - July 6, 2017
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StringMatching
{
/// <summary>
/// Leetcode 10: Regular Expression Matching
/// </summary>
class Program
{
static void Main(string[] args)
{
var result = IsMatch("aa", "aa");
}
public static bool IsMatch(string text, string pattern)
{
if (text == null || pattern == null)
{
return false;
}
if (pattern.Length == 0)
{
return text.Length == 0;
}
var test_pattern = pattern[0]; //#
bool isStar = pattern.Length > 1 && pattern[1] == '*';
bool firstCharMatching = text.Length > 0 &&
(test_pattern == '.' || text[0] == test_pattern);
if (pattern.Length > 1 && isStar)
{
return IsMatch(text, pattern.Substring(2)) ||
(firstCharMatching &&IsMatch(text.Substring(1), pattern));
}
return firstCharMatching && IsMatch(text.Substring(1), pattern.Substring(1));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment