codewars048: Triple Double

Instructions

Triple Doublehtml

Solutions

import java.util.regex.*;
public class TripleAndDouble{
    public static int TripleDouble(long num1, long num2){
        String str1 = String.valueOf(num1);
        String regex = "\\d*([0-9])(\\1){2}\\d*";
        if(!str1.matches(regex)){
            return 0;
        }    
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str1);
        while(m.find()){
            String d = m.group(1);
            String str2 = String.valueOf(num2);
            if(str2.contains(d + d)){
                return 1;
            }    
        }    
        return 0;
    }    
}

Example Tests

import org.junit.Test;
import org.junit.Assert.assertEquals;
public class TripleDoubleTest{
    @Test
    public void test1(){
        assertEquals(1, TripleAndDouble(451999277L, 41177722899L));
    }    
    @Test
    public void test2(){
        assertEquals(0, TripleAndDouble(1222345L, 12345L));
    }   
    @Test
    public void test3(){
        assertEquals(0, TripleAndDouble(12345L, 12345L));
    } 
    @Test
    public void test4(){
        assertEquals(1, TripleAndDoubl666789L, 12345667L));
    } 
}

Reference

同一個字母連續重複三次以上,正則表達式怎麼寫?java

相關文章
相關標籤/搜索