Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class ThreeCharacters {
- public static boolean threeCharMatch(String s, String t) {
- if (s.length() < 3 || t.length() < 3) return true;
- else if (s.length() == 3 && t.length() == 3 && s.equals(t)) return false;
- for (int i = 0; i + 3 <= s.length(); i++) {
- String ss = s.substring(i, i + 3);
- Pattern p = Pattern.compile(Pattern.quote(ss));
- Matcher m = p.matcher(t);
- if (m.find()) return false;
- }
- return true;
- }
- public static void main(String[] args) {
- String a = "a";
- String b = "b";
- System.out.println(a + ", " + b + ": " + threeCharMatch(a, b));
- a = "ab";
- b = "bc";
- System.out.println(a + ", " + b + ": " + threeCharMatch(a, b));
- a = "abc";
- b = "abc";
- System.out.println(a + ", " + b + ": " + threeCharMatch(a, b));
- a = "abcd";
- b = "bcde";
- System.out.println(a + ", " + b + ": " + threeCharMatch(a, b));
- a = "abcd";
- b = "wxyz";
- System.out.println(a + ", " + b + ": " + threeCharMatch(a, b));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement