title: How do I find the percentage of similarity between two multiline Strings?
body: <p>I have got two multi-line strings. I'm using the following code to determine the similarity between two of them.  This makes use of Levenshtein distance algorithm.</p>
  
  <pre><code>  public static double similarity(String s1, String s2) {
      String longer = s1, shorter = s2;
      if (s1.length() &lt; s2.length()) { 
        longer = s2; shorter = s1;
      }
      int longerLength = longer.length();
      if (longerLength == 0) { return 1.0; /* both strings are zero length */ }
  
      return (longerLength - editDistance(longer, shorter)) / (double) longerLength;
  
    }
  
    public static int editDistance(String s1, String s2) {
      s1 = s1.toLowerCase();
      s2 = s2.toLowerCase();
  
      int[] costs = new int[s2.length() + 1];
      for (int i = 0; i &lt;= s1.length(); i++) {
        int lastValue = i;
        for (int j = 0; j &lt;= s2.length(); j++) {
          if (i == 0)
            costs[j] = j;
          else {
            if (j &gt; 0) {
              int newValue = costs[j - 1];
              if (s1.charAt(i - 1) != s2.charAt(j - 1))
                newValue = Math.min(Math.min(newValue, lastValue),
                    costs[j]) + 1;
              costs[j - 1] = lastValue;
              lastValue = newValue;
            }
          }
        }
        if (i &gt; 0)
          costs[s2.length()] = lastValue;
      }
      return costs[s2.length()];
    }
  </code></pre>
  
  <p>But the above code is not working as expected. </p>
  
  <p>For instance lets say that we have got the following two strings say <code>s1</code> and <code>s2</code>,</p>
  
  <p>S1 -> <code>How do we optimize  the performance? . What should we do to compare both strings to find the percentage of similarity between both?</code></p>
  
  <p>S2-> <code>How do we optimize tje performance? What should we do to compare both strings to find the percentage of similarity between both?</code></p>
  
  <p>Then I'm passing the above string to similarity method but it does not find the exact percentage of difference. How do I optimize the algorithm?</p>
  
  <p>Following is my main method</p>
  
  <p><strong>update</strong>:</p>
  
  <pre><code>public static boolean authQuestion(String question) throws SQLException{
  
  
          boolean isQuestionAvailable = false;
          Connection dbCon = null;
          try {
              dbCon = MyResource.getConnection();
              String query = "SELECT * FROM WORDBANK where WORD ~*  ?;";
              PreparedStatement checkStmt = dbCon.prepareStatement(query);
              checkStmt.setString(1, question);
              ResultSet rs = checkStmt.executeQuery();
              while (rs.next()) {
                  double re=similarity( rs.getString("question"), question);
                  if(re  &gt; 0.6){
                      isQuestionAvailable = true;
                  }else {
                      isQuestionAvailable = false;
                  }
              }
          } catch (URISyntaxException e1) {
              e1.printStackTrace();
          } catch (SQLException sqle) {
              sqle.printStackTrace();
          } catch (Exception e) {
              if (dbCon != null)
                  dbCon.close();
          } finally {
              if (dbCon != null)
                  dbCon.close();
          }
  
          return isQuestionAvailable;
      }
  </code></pre>