Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8891677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:48:18+00:00 2026-06-14T22:48:18+00:00

I want to use the Levenshtein algorithm for the following task: if a user

  • 0

I want to use the Levenshtein algorithm for the following task: if a user on my website searches for some value (he enters characters in a input), I want to instantly check for suggestions with AJAX, like Google Instant does.

I have the impression that the Levenshtein algorithm is way too slow for such a task. To check its behaviour, I first implemented it in Java, printing out the two Strings in every recursive call of the method.

public class Levenshtein {
    public static void main(String[] arg){
        String a = "Hallo Zusammen";
        String b = "jfdss Zusammen";

        int res = levenshtein(a, b);

        System.out.println(res);
    }

    public static int levenshtein(String s, String t){
        int len_s = s.length();
        int len_t = t.length();
        int cost = 0;

        System.out.println("s: " + s + ", t: " + t);

        if(len_s>0 && len_t>0){
            if(s.charAt(0) != t.charAt(0)) cost = 1;
        }

        if(len_s == 0){
            return len_t;
        }else{
            if(len_t == 0){
                return len_s;
            }else{
                String news = s.substring(0, s.length()-1);
                String newt = t.substring(0, t.length()-1);
                return min(levenshtein(news, t) + 1,
                            levenshtein(s, newt) + 1,
                            levenshtein(news, newt) + cost);
            }
        }
    }

    public static int min(int a, int b, int c) {
          return Math.min(Math.min(a, b), c);
    }
}

However, here are some points:

  • The check if(len_s>0 && len_t>0) was added by me, because I was getting a StringIndexOutOfBoundsException with above test values.
  • With above test values, the algorithm seems to calculate infinitely

Are there optimizations that can be made on the algorithm to make it work for me, or should I use a completely different one to accomplish the desired task?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:48:20+00:00Added an answer on June 14, 2026 at 10:48 pm

    1) Few words about Levenshtein distance algorithm improvement

    Recursive implementation of Levenshteins distance has exponential complexity.

    I’d suggest you to use memoization technique and implement Levenshtein distance without recursion, and reduce complexity to O(N^2)(needs O(N^2) memory)

    public static int levenshteinDistance( String s1, String s2 ) {
        return dist( s1.toCharArray(), s2.toCharArray() );
    }
    
    public static int dist( char[] s1, char[] s2 ) {
    
        // distance matrix - to memoize distances between substrings
        // needed to avoid recursion
        int[][] d = new int[ s1.length + 1 ][ s2.length + 1 ];
    
        // d[i][j] - would contain distance between such substrings:
        // s1.subString(0, i) and s2.subString(0, j)
     
        for( int i = 0; i < s1.length + 1; i++ ) {
            d[ i ][ 0 ] = i;
        }
     
        for(int j = 0; j < s2.length + 1; j++) {
            d[ 0 ][ j ] = j;
        }
     
        for( int i = 1; i < s1.length + 1; i++ ) {
            for( int j = 1; j < s2.length + 1; j++ ) {
                int d1 = d[ i - 1 ][ j ] + 1;
                int d2 = d[ i ][ j - 1 ] + 1;
                int d3 = d[ i - 1 ][ j - 1 ];
                if ( s1[ i - 1 ] != s2[ j - 1 ] ) {
                    d3 += 1;
                }
                d[ i ][ j ] = Math.min( Math.min( d1, d2 ), d3 );
            }
        }
        return d[ s1.length ][ s2.length ];
    }
    

    Or, even better – you may notice, that for each cell in distance matrix – you need only information about previous line, so you can reduce memory needs to O(N):

    public static int dist( char[] s1, char[] s2 ) {
    
        // memoize only previous line of distance matrix     
        int[] prev = new int[ s2.length + 1 ];
     
        for( int j = 0; j < s2.length + 1; j++ ) {
            prev[ j ] = j;
        }
     
        for( int i = 1; i < s1.length + 1; i++ ) {
    
            // calculate current line of distance matrix     
            int[] curr = new int[ s2.length + 1 ];
            curr[0] = i;
     
            for( int j = 1; j < s2.length + 1; j++ ) {
                int d1 = prev[ j ] + 1;
                int d2 = curr[ j - 1 ] + 1;
                int d3 = prev[ j - 1 ];
                if ( s1[ i - 1 ] != s2[ j - 1 ] ) {
                    d3 += 1;
                }
                curr[ j ] = Math.min( Math.min( d1, d2 ), d3 );
            }
    
            // define current line of distance matrix as previous     
            prev = curr;
        }
        return prev[ s2.length ];
    }
    

    2) Few words about autocomplete

    Levenshtein’s distance is preferred only if you need to find exact matches.

    But what if your keyword would be apple and user typed green apples? Levenshteins distance between query and keyword would be large (7 points). And Levensteins distance between apple and bcdfghk (dumb string) would be 7 points too!

    I’d suggest you to use full-text search engine (e.g. Lucene). The trick is – that you have to use n-gram model to represent each keyword.

    In few words:

    1) you have to represent each keyword as document, which contains n-grams: apple -> [ap, pp, pl, le].

    2) after transforming each keyword to set of n-grams – you have to index each keyword-document by n-gram in your search engine. You’ll have to create index like this:

    ...
    ap -> apple, map, happy ...
    pp -> apple ...
    pl -> apple, place ...
    ...
    

    3) So you have n-gram index. When you get a query – you have to split it into n-grams. After this – you’ll have a set of user’s query n-grams. And all you need – is to match most similar documents from your search engine. In draft approach it would be enough.

    4) For better suggest – you may rank results of search-engine by Levenshtein distance.

    P.S. I’d suggest you to look through the book "Introduction to information retrieval".

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use Levenshtein algorithm to search in a list of strings. I
i want use some data from a website with web service. i have a
I have some properties defined by the user, and then I want use them
I want to use a select element for mobile viewing of my website. Following
I'd like to use the Levenshtein algorithm to compare two files in VB.NET. I
I want use regex to make a judge, if some sring's structure is made
I want use page that has this form: <form method=post action=modules.php?name=search> <input onkeypress=FKeyPress(this); onkeydown=FKeyDown(this);
I want use ajax.net to do some js. like below: ScriptManager.RegisterStartupScript(Submit, typeof(Button), alert, location.href='test.aspx';,
I want use ORDER BY RAND() query mysql. But I have some question want
I want use $.ajax to read some infomation from xml file,here is my js

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.