问题描述:

I have two strings containing some hex values.

String One = "f9 d4 62 aa f9 d4 62 aa 74 10 99 8b";

String Two = "3c 9c a7 e2 3c 9c a7 e2 b1 58 f9 d4";

Please note that the spaces in between the hex is generated by function that is converting binary to hex.Furthermore, the hex values are treated in pairs eg, f9 d4 is regarded as one pair.

My question is - how can I return the index (in this case, substring value) of two similar pair in the two strings?

The above example has f9 d4 in index 1 while the other String has it on index 6. So i want my output as (1,6) and (3,6).

Any help or suggestion would be appreciated.

网友答案:

Consider such code:

public static void main(String[] args) {
    String One = "f9 d4 62 aa f9 d4 62 aa 74 10 99 8b";
    String Two = "3c 9c a7 e2 3c 9c a7 e2 b1 58 f9 d4";

    int[] x = convert(One);
    int[] y = convert(Two);

    for (int i = 0; i < x.length; i++) {
        int number = x[i];
        int index = find(number, y);
        if (index > 0) {
            System.out.println("found (" + (i + 1) + "," + index + ")");
        }
    }

}

private static int find(int number, int[] array) {
    for (int i = 0; i < array.length; i++) {
        if (array[i] == number) {
            return i + 1;
        }
    }
    return 0;
}

private static int[] convert(String str) {

    String[] tokens = str.split("\\s");
    int[] result = new int[tokens.length / 2];

    for (int i = 0; i < tokens.length; i += 2) {
        String hex = tokens[i] + tokens[i + 1];
        result[i / 2] = Integer.parseInt(hex, 16);
    }

    return result;
}

Output:

found (1,6)
found (3,6)

As you can see convert(str) method converts each 4 hex digits into 1 integer and returns array of such integers. So convert(One) is simply int[] equal to:

System.out.println(Arrays.toString(x));
[63956, 25258, 63956, 25258, 29712, 39307]

Next you can implement helper method find() that returns index where number is found in given array (1-based index).

网友答案:

hope it help !

String one = "f9 d4 62 aa f9 d4 62 aa 74 10 99 8b";
String two = "3c 9c a7 e2 3c 9c a7 e2 b1 58 f9 d4";

String[] oneArr = one.split(" ");
String[] twoArr = two.split(" ");

ArrayList<String> results = new ArrayList<>();      

for(int i = 0, countOne = 0 ; i < oneArr.length - 1 ; i = i + 2,countOne++) {

    String hexCoupleOne = oneArr[i] + " " + oneArr[i + 1];

    if(two.contains(hexCoupleOne)) {
        //searching index in two . . .
        for(int j = 0, countTwo = 0  ; j < twoArr.length - 1 ; j = j + 2, countTwo++) {
            String hexCoupleTwo = twoArr[j] + " " + twoArr[j + 1];
            if(hexCoupleOne.equals(hexCoupleTwo))
                results.add((countOne + 1) + "," + (countTwo + 1));
        }           
    }       
}

System.out.println("total pair : "+results.size());
for(String res : results) {
    System.out.println("Found a pair at index="+res.split(",")[0]+" in String one and at index="+res.split(",")[1]+" in String two." );
}
网友答案:

More efficient approach is not to search second string each time, but instead transform it into more suitable data structure, such as hash map where key is string pair and value is the list of positions when this pair appears. This complexity of such approach is O(n), comparing to O(n2) when you search second string each time. This difference will be significant when you have bigger input strings.

    /**
     * Splits argument into substrings of lengths 6 and puts them into Map
     * where key is substring and value is the list of positions where substring appears 
     * in original string.
     * @param str string to split
     * @return Map of positions
     */
    private static Map<String, List<Integer>> indexMapOfPairs(String str) {
        Map<String, List<Integer>> result = new HashMap<String, List<Integer>>();
        for (int i = 0; i < str.length(); i += 6) {
            String pair = str.substring(i, i + 5);
            List<Integer> indexList = result.get(pair);
            if (indexList == null) {
                indexList = new ArrayList<Integer>(4);
                result.put(pair, indexList);
            }
            indexList.add(i / 6 + 1);
        }
        return result;
    }

    public static void main(String[] args) {
        String one = "f9 d4 62 aa f9 d4 62 aa 74 10 99 8b";
        String two = "3c 9c a7 e2 3c 9c a7 e2 b1 58 f9 d4";

        Map<String, List<Integer>> oneAsMap = indexMapOfPairs(one);
        Map<String, List<Integer>> twoAsMap = indexMapOfPairs(two);

        for (Map.Entry<String, List<Integer>> oneEntry : oneAsMap.entrySet()) {
            String pair = oneEntry.getKey();
            List<Integer> twoIndices = twoAsMap.get(pair);
            if (twoIndices != null) {
                for (Integer oneIndex : oneEntry.getValue()) {
                    for (Integer twoIndex : twoIndices) {
                        System.out.printf("(%d, %d)%n", oneIndex, twoIndex);
                    }
                }
            }
        }
    }
相关阅读:
Top