Concise c# solution


  • 0
    M
    public class Solution {
        public bool IsMatch(string s, string p) {
            var prevMatches = new bool[s.Length+1];
            prevMatches[0] = true;
            
            for(int i = 0; i < p.Length; i++) {
                var matches = new bool[s.Length+1];
                
                for(int j = 0; j < s.Length+1; j++) {
                    if(p[i] == '*') {
                        matches[j] = prevMatches[j] || (j > 0 && matches[j-1]);
                    } else{
                        matches[j] = j > 0 && prevMatches[j-1] && (p[i] == '?' || p[i] == s[j-1]);
                    }
                }
                
                prevMatches = matches;
            }
            
            return prevMatches[s.Length];
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.