Advertisement
cd62131

ThreeCharacters

Mar 11th, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.09 KB | None | 0 0
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. public class ThreeCharacters {
  5.   public static boolean threeCharMatch(String s, String t) {
  6.     if (s.length() < 3 || t.length() < 3) return true;
  7.     else if (s.length() == 3 && t.length() == 3 && s.equals(t)) return false;
  8.     for (int i = 0; i + 3 <= s.length(); i++) {
  9.       String ss = s.substring(i, i + 3);
  10.       Pattern p = Pattern.compile(Pattern.quote(ss));
  11.       Matcher m = p.matcher(t);
  12.       if (m.find()) return false;
  13.     }
  14.     return true;
  15.   }
  16.  
  17.   public static void main(String[] args) {
  18.     String a = "a";
  19.     String b = "b";
  20.     System.out.println(a + ", " + b + ": " + threeCharMatch(a, b));
  21.     a = "ab";
  22.     b = "bc";
  23.     System.out.println(a + ", " + b + ": " + threeCharMatch(a, b));
  24.     a = "abc";
  25.     b = "abc";
  26.     System.out.println(a + ", " + b + ": " + threeCharMatch(a, b));
  27.     a = "abcd";
  28.     b = "bcde";
  29.     System.out.println(a + ", " + b + ": " + threeCharMatch(a, b));
  30.     a = "abcd";
  31.     b = "wxyz";
  32.     System.out.println(a + ", " + b + ": " + threeCharMatch(a, b));
  33.   }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement