Open In App

Java Program to Implement Commentz-Walter Algorithm

The Commentz-Walter algorithm is a string-matching algorithm that is used to search for a given pattern in a text string. 

Examples:



Input: str = “Hello, World! How are you?”,  pattern = “World”
Output: Pattern found at index 7

Input: str = “Hello, World! How are you?”,  pattern = “you”
Output: Pattern found at index 22



Applications of Commentz-Walter Algorithm: 

The Commentz-Walter algorithm is a string-matching algorithm that can be used to search for a pattern in a text string. Some possible applications of this algorithm include:

These are just a few examples of how the Commentz-Walter algorithm can be used. It can be applied to any situation where it is necessary to search for a pattern the in a text string.

Follow the below steps to solve the algorithm:

The Commentz-Walter is a string-matching algorithm that uses a failure function to skip over parts of the text string that cannot match the pattern string. This can be a faster and more efficient way to search for a pattern in a text string compared to other string-matching algorithms.

Here is an example of how the Commentz-Walter algorithm works:

Let’s say we have the following text string: Hello, World! How are you? and we want to search for the pattern string World in the text string

Below is the Implementation of the above approach:




import java.io.*;
public class CommentzWalter {
    // Function to find the pattern in the text string using
    // the Commentz-Walter algorithm
    public static int search(String text, String pattern)
    {
        // Preprocess the pattern string to create the
        // failure function
        int[] failure = createFailureFunction(pattern);
  
        // Initialize the pointers for the text and pattern
        // strings
        int i = 0;
        int j = 0;
  
        // Loop through the text string
        while (i < text.length()) {
  
            // Check if the current character of the text
            // string matches the current character of the
            // pattern string
            if (text.charAt(i) == pattern.charAt(j)) {
  
                // If the characters match, increment the
                // pointers for both strings
                i++;
                j++;
            }
  
            // If the pattern string has been matched,
            // return the starting index of the match
            if (j == pattern.length()) {
                return i - j;
            }
  
            // If the current character of the text string
            // does not match the current character of the
            // pattern string, use the failure function to
            // skip over part of the text string that cannot
            // match the pattern
            if (i < text.length()
                && text.charAt(i) != pattern.charAt(j)) {
                if (j > 0) {
                    j = failure[j - 1];
                }
                else {
                    i++;
                }
            }
        }
  
        // If the pattern string was not found
        // in the text string, return -1
        return -1;
    }
  
    // Function to preprocess the pattern
    // string and create the failure function
    public static int[] createFailureFunction(
        String pattern)
    {
        int[] failure = new int[pattern.length()];
  
        // Initialize the pointers for the
        // pattern string
        int i = 0;
        int j = 1;
  
        // Loop through the pattern string
        while (j < pattern.length()) {
  
            // If the current character of the pattern
            // string matches the previous character,
            // increment the pointers for both strings
            if (pattern.charAt(i) == pattern.charAt(j)) {
                failure[j] = i + 1;
                i++;
                j++;
            }
            else {
  
                // If the characters do not match, use the
                // failure function to skip over part of the
                // pattern string that cannot match the text
                // string
                if (i > 0) {
                    i = failure[i - 1];
                }
                else {
                    failure[j] = 0;
                    j++;
                }
            }
        }
  
        return failure;
    }
  
    public static void main(String[] args)
    {
  
        // Text string
        String text = "Hello, World! How are you?";
  
        // Pattern string
        String pattern = "World";
  
        // Search for the pattern in the text
        // string using the Commentz-Walter algorithm
        int index = search(text, pattern);
  
        // Print the result
        if (index >= 0) {
            System.out.println("Pattern found at index "
                               + index);
        }
        else {
            System.out.println(
                "Pattern not found in the text string");
        }
    }
}

Output
Pattern found at index 7

Time complexity: O(M+N), where M is the length of the pattern and N is the length of the text string. 
Auxiliary Space: O(N), where N is the length of the pattern string

Related Articles:


Article Tags :