[Wildcard Matching -Java]Time Limit Exceeded in regex expression


  • 0
    C

    if you put an 'a' at the end of the first string, it would give you the answer immediately, but without 'a' it wouldn't give you an answer. Not just for solving the problem, but also for figuring out the Regex Expression of java

      package leetcode;
        import java.util.*;
        public class Solution {
        	public static void main(String arg[]) {
        		Solution s = new Solution();
        		/*if you put an 'a' at the end of the first string, 
        		 * it would give you the answer immediately, but
        		*without 'a' it wouldn't give you an answer.
        		*/
        		System.out
        				.print(s.isMatch(
        						"abbabaaabbabbaababbabbbbbabbbabbbabaaaaabababab"
        						+ "bbabababaabbababaabbbbbbaaaabababbbaabbbbaabbb"
        						+ "bababababbaabbaababaabbbababababbbbaaabbbbbabaa"
        						+ "aabbababbbbaababaabbababbbbbababbbabaaaaaaaabbb"
        						+ "bbaabaaababaaaabb",
        						"**aa*****ba*a*bb**aa*ab****a*aaaaaa***a*aaaa**bbabb"));
        	}
        
        	public boolean isMatch(String s, String p) {
        		String p1 = p.replace("?", "\\S");
        		String p2 = p1.replaceAll("[\\*]+", "[\\\\S]*");
        		System.out.println(p2);
        		return s.matches(p2);
        	}
        }

Log in to reply
 

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