Friday, July 3, 2020

Leetcode discuss: Leetcode 10: regular expression

Here is the link. 

Case study - mock interview as an interviewer on July 2, 2017

July 2, 2020

Introduction
It is a good idea to review what I learned as an interviewer and then write a post after three years. Here is the blog about mock interview.

The following is C# code to pass online judge.

public class Solution {
    //public bool IsMatch(string s, string p) {
    public 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));                 
            }
            else if (firstCharMatching)
            {
                return IsMatch(text.Substring(1), pattern.Substring(1));
            }
            else
            {
                return false; 
            }
        }
}


No comments:

Post a Comment