Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package levenshtein;
- import javax.swing.JOptionPane;
- public class Levenshtein {
- private String s1 = new String();
- private String s2 = new String();
- private int n;
- // métodos
- public void set(){
- s1 = JOptionPane.showInputDialog("Digite o texto 1:");
- s2 = JOptionPane.showInputDialog("Digite o texto 2:");
- }
- public void print() {
- n=distancia();
- JOptionPane.showMessageDialog(null, "Número mínimo de operações: " + n);
- }
- public int distancia() {
- int n1 = s1.length();
- int n2 = s2.length();
- int i, j;
- int t[] = new int[n2 + 1];
- for (i = 0; i <= n2; i++) {
- t[i] = i;
- }
- for (i = 0; i < n1; i++) {
- t[0] = i + 1;
- int canto = i;
- for (j = 0; j < n2; j++) {
- int upper = t[j + 1];
- if (s1.charAt(i) == s2.charAt(j)) {
- t[j + 1] = canto;
- } else {
- t[j + 1] = Math.min(t[j], Math.min(upper, canto)) + 1;
- }
- canto = upper;
- }
- }
- return t[n2];
- }
- public static void main(String[] args) {
- Levenshtein v = new Levenshtein();
- v.set();
- v.print();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement