如何查找字符串中两个字词之间的距离

时间:2019-05-29 00:34:41

标签: java

要求:给定字符串,找到所有出现的字谜之间的距离 示例:“ programmerxxddporragmmerbbffprogrammer” 字符串pat =“程序员”;

预期输出:4 “程序员”的两个回文之间的距离是4

//Java program to search all anagrams 
//of a pattern in a text 
public class Pattern
{ 
    static final int MAX = 256; 

    // This function returns true if contents 
    // of arr1[] and arr2[] are same, otherwise 
    // false. 
    static boolean compare(char arr1[], char arr2[]) 
    { 
        for (int i = 0; i < MAX; i++) 
            if (arr1[i] != arr2[i]) 
                return false; 
        return true; 
    } 

    // This function search for all permutations 
    // of pat[] in txt[] 
    static void search(String pat, String txt) 
    { 
        int M = pat.length(); 
        int N = txt.length(); 

        // countP[]: Store count of all 
        // characters of pattern 
        // countTW[]: Store count of current 
        // window of text 
        char[] countP = new char[MAX]; 
        char[] countTW = new char[MAX]; 
        for (int i = 0; i < M; i++) 
        { 
            (countP[pat.charAt(i)])++; 
            (countTW[txt.charAt(i)])++; 
        } 

        // Traverse through remaining characters 
        // of pattern 
        for (int i = M; i < N; i++) 
        { 
            // Compare counts of current window 
            // of text with counts of pattern[] 
            if (compare(countP, countTW)) 
                System.out.println("Found at Index " + 
                                        (i - M)); 

            // Add current character to current 
            // window 
            (countTW[txt.charAt(i)])++; 

            // Remove the first character of previous 
            // window 
            countTW[txt.charAt(i-M)]--; 
        } 

        // Check for the last window in text 
        if (compare(countP, countTW)) {
            System.out.println("Found at Index " + 
                                    (N - M)); 
            System.out.println(N-M-M);
        }
    } 

    /* Driver program to test above function */
    public static void main(String args[]) 
    { 
        String txt = "programmerxxddporragmmerbbffprogrammer"; 
        String pat = "programmer"; 
        search(pat, txt); 
    } 
} 

我需要打印两个字谜之间差异的第一个实例。就我而言,是4.我的代码正在打印最终的字符串,如下所示:

1 个答案:

答案 0 :(得分:0)

因此您的代码在查找回文方面表现良好。因此,我对其进行了调整,以跟踪找到最后一个回文的位置,以便在找到新回文时,可以采用两者之间的距离,该距离即为每个索引的下标减去图案的长度(我选择跟踪所有差异,但是您可以禁止这样做)。这是适用于每次报告距离的代码(其余部分保持不变)。

//Java program to search all anagrams 
//of a pattern in a text 
public class Pattern{
static final int MAX = 256; 

// This function returns true if contents 
// of arr1[] and arr2[] are same, otherwise 
// false. 
static boolean compare(char arr1[], char arr2[]) 
{ 
    for (int i = 0; i < MAX; i++) 
        if (arr1[i] != arr2[i]) 
            return false; 
    return true; 
} 

// This function search for all permutations 
// of pat[] in txt[] 
static void search(String pat, String txt) 
{ 
    int M = pat.length(); 
    int N = txt.length();
    int lastFoundIndex = -1;

    // countP[]: Store count of all 
    // characters of pattern 
    // countTW[]: Store count of current 
    // window of text 
    char[] countP = new char[MAX]; 
    char[] countTW = new char[MAX]; 
    for (int i = 0; i < M; i++) 
    { 
        (countP[pat.charAt(i)])++; 
        (countTW[txt.charAt(i)])++; 
    } 

    // Traverse through remaining characters 
    // of pattern 
    for (int i = M; i < N; i++) 
    { 
        // Compare counts of current window 
        // of text with counts of pattern[] 
        if (compare(countP, countTW)) {
            System.out.println("Found at Index " + 
                                    (i - M)); 
                                    if (lastFoundIndex==-1){
            lastFoundIndex = i-M;}
            else {
            System.out.println("Distance between is: "+(i-M-lastFoundIndex-pat.length()));
            lastFoundIndex = i-M;

        }
        }


        // Add current character to current 
        // window 
        (countTW[txt.charAt(i)])++; 

        // Remove the first character of previous 
        // window 
        countTW[txt.charAt(i-M)]--; 
    } 

    // Check for the last window in text 
    if (compare(countP, countTW)) {
        System.out.println("Found at Index " + 
                                (N - M));
        if (lastFoundIndex==-1){
            lastFoundIndex = N-M;
        }
        else{

            System.out.println("Distance between is: "+(N-M-lastFoundIndex-pat.length()));
            lastFoundIndex = N-M;
        }
    }
} 

/* Driver program to test above function */
public static void main(String args[]) 
{ 
    String txt = "programmerxxddporragmmerbbffprogrammer"; 
    String pat = "programmer"; 
    search(pat, txt); 
} 
}

输出是

Found at Index 0
Found at Index 14
Distance between is: 4
Found at Index 28
Distance between is: 4

版本: 然后,我回头以不同的方式实施了整个过程,可能效率较低,但对我来说更容易理解(输出是相同的)。

//Java program to search all anagrams 
//of a pattern in a text 
import java.util.Arrays;
public class Pattern {
    // Method to sort a string alphabetically (from https://www.geeksforgeeks.org/sort-a-string-in-java-2-different-ways/) 
    public static String sortString(String inputString) {
        // convert input string to char array 
        char tempArray[] = inputString.toCharArray();

        // sort tempArray 
        Arrays.sort(tempArray);

        // return new sorted string 
        return new String(tempArray);
    }

    // This function searches for all permutations 
    // of pat in txt 
    static void search(String pat, String txt) {
        String patSorted = sortString(pat); //sort the pattern once
        int M = pat.length();
        int N = txt.length();
        int lastFoundIndex = -1; //last place found
        for (int i = 0; N - i >= M; i++) { //while there are still enough remaining characters from this index on in txt
            if (sortString(txt.substring(i, i + M)).equals(patSorted)) {
                System.out.println("Found at Index " + i);
                if (lastFoundIndex == -1) {
                    lastFoundIndex = i;
                } else {
                    System.out.println("Distance between is: " + (i - lastFoundIndex - M));
                    lastFoundIndex = i;
                }
            }

        }

    }



    /* Driver program to test above function */
    public static void main(String args[]) {
        String txt = "programmerxxddporragmmerbbffprogrammer";
        String pat = "programmer";
        search(pat, txt);
    }
}