如何在char数组中找到重复模式?

时间:2013-09-21 18:38:35

标签: java arrays

我有一个字符数组:

a b c x y d e f x y a b c t r e a b c

如何找到尺寸为2的重复图案?

需要从末尾遍历数组。在示例中,我需要找到模式b ca bx y和大小为3的模式:a b cx y z。以及匹配字符的索引。

到目前为止,我试图向后遍历数组并找到模式:

for (int size = 2; size < aLT.size(); size++) {
    for (int i = aLT.size() - 1; i >= 0; i--) {
        // code here
    }
}

3 个答案:

答案 0 :(得分:0)

这将完成这项工作,您可以将patternSize变量更改为您想要的任何值(远小于输入字符串的大小):

利用String#contains()方法查找第一个String的子序列。

    public static void main(String[] args) {
    int patternSize=4;
    String input = "abcxydefxyabctreabcabcx";
    Set<String> patterns = new TreeSet<String>();

    // test size n patterns
    for (int i=0; i<input.length()-patternSize; i++){
        String pattern = (String) input.subSequence(i, i+patternSize);
        String tester="";
        if (i>0 && i<input.length()-patternSize-1)
            tester = input.substring(0,i)+input.substring(i+patternSize);
        else if (i==0)
            tester = input.substring(i+patternSize);
        else if (i==input.length()-patternSize-1)
            tester = input.substring(0,i);

        if (tester.contains(pattern)){
            patterns.add(pattern);
        }
    }   

    System.out.println("Size "+patternSize+" patterns finder");
    for(String aPattern : patterns){
        System.out.println("The pattern "+aPattern+" was found several times");
    }
}

答案 1 :(得分:0)

int n = 2; // in your case 2 and 3
Map<String, List<Integer>> matches = new HashMap<String, List<Integer>>();
String charsString = new String( chars );
String current = null;
String rest = null;
for( int i = chars.length - n; i >= 0; i-- ) {
    current = charsString.substring( i, i + n );
    rest = charsString.substring( 0, i );
    int index = rest.indexOf( current );
    if( index > -1 ) {
        if( matches.containsKey( current ) ) {
            continue;
        }
        List<Integer> indices = new ArrayList<Integer>();
        indices.add( i );
        while( index > -1 ) {
            indices.add( index );
            index = rest.indexOf( current, index + 1 );
        }
        matches.put( current, indices );
    }
}

// print the results
for( Entry<String, List<Integer>> match : matches.entrySet() ) {
    System.out.println( match.getKey() + " with indices: " + match.getValue() );
}

输出是:

ab with indices: [16, 0, 10]
bc with indices: [17, 1, 11]
xy with indices: [8, 3]

答案 2 :(得分:0)

这是一种做你想做的事情的方法。如果你想要改变patternSize和添加到集合中的字符串,你需要做的不同尺寸的图案。目前我让它返回匹配数量的计数,但你可以轻松地修改它以返回其他内容,例如匹配开始位置的索引或是否有任何匹配的布尔值。

    public static int findPatterns(char[] charArray) {
    int patternSize = 2;
    Set<String> patterns = new HashSet<>();
    patterns.add("bc");
    patterns.add("ab");
    patterns.add("xy");
    int count = 0;
    if (charArray.length < patternSize) {
        return 0;
    }
    for (int i = 0; i < charArray.length - patternSize + 1; i++) {
        String pattern = "";
        for (int j = i; j < i + patternSize; j++) {
            pattern += charArray[j];
        }
        if (patterns.contains(pattern)) {
            count++;
        }
    }
    return count;
}