GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Algorithm-Implementations/StringMatch.java at master · devForTheFuture/Algorithm-Implementations Expand file tree/ Copy pathStringMatch.javaMore file actions 94 lines (85 loc) · 2.05 KB / Copy pathStringMatch.javaFile metadata and controls94 lines (85 loc) · 2.05 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 package algorithms; /** * Given a regular expression with characters a-z, ' * ', ' . ' * the task was to find if that string could match another string with characters from: a-z * where ' * ' can delete the character before it, and '.' could match whatever character. '*' always appear after a a-z character. * Example: * isMatch("a*", "") = true; * isMatch(".", "") = false; * isMatch("ab*", "a") = true; * isMatch("a.", "ab") = true; * isMatch("a", "a") = true; * * @author joeytawadrous */ public class StringMatch { public static void main(String[] args) { String s1 = "a*abc"; String s2 = "abc"; System.out.println(isMatch(s1, s2, 0, 0)); String s3 = "a*a*a"; String s4 = "a"; System.out.println(isMatch(s3, s4, 0, 0)); String s5 = "a."; String s6 = "ab"; System.out.println(isMatch(s5, s6, 0, 0)); String s7 = "a*bd*c"; String s8 = "abdc"; System.out.println(isMatch(s7, s8, 0, 0)); String s9 = "a."; String s10 = "s"; System.out.println(isMatch(s9, s10, 0, 0)); } /** * Checks * @param s1 * @param s2 * @param posS1 * @param posS2 * @return boolean */ public static boolean isMatch(String s1, String s2, int posS1, int posS2) { int l1 = s1.length(); int l2 = s2.length(); // System.out.println("posS1:"+posS1+" posS2:"+posS2); if(posS1 > l1 || posS2 > l2) { return false; } else if(posS1 == l1 && posS2 == l2) { return true; } else { if(posS2 < l2 && posS1 < l1 && ((s1.charAt(posS1) == s2.charAt(posS2)) || s1.charAt(posS1) == '.')) { return isMatch(s1, s2, posS1+1, posS2+1); } else if(posS1 < l1 && s1.charAt(posS1) == '*') { if(posS2 > 0) { return isMatch(s1, s2, posS1+1, posS2-1) || isMatch(s1, s2, posS1+1, posS2); } else { return isMatch(s1, s2, posS1+1, posS2); } } else { if(posS1+1 < l1 && s1.charAt(posS1+1) == '*') { return isMatch(s1, s2, posS1+2, posS2); } else { return false; } } } } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |