Calculation of word similarity with Java

Asked

Viewed 467 times

0

For two string A and B, we define the similarity of these strings is the length of the prefix that is common to both. For example, the similarity of strings abc and abd is 2, while the similarity of strings aaa and aaab is 3.

Calculate the sum of similarities of a string S with each of its suffixes, including its own string as the first suffix.

Input Format:

The first line contains the number of test cases T. Each of the T lines next to it contains a string each.

Output Format:

Display T output lines, each containing an integer number that is for the corresponding test case.

Premises:

  • 1 <= T <= 10
  • As strings contains only lowercase characters [a-z]

Input example:

2
ababaa
aa

Output example:

11
3

Explanation:

For the first case, the suffixes of string sane ababaa, babaa, abaa, baa, aa and a. The similarities of each of these string with the string ababaa are 6, 0, 3, 0, 1, 1 respectively. So the answer is 6 + 0 + 3 + 0 + 1 + 1 = 11. For the second case the answer is 2 + 1 = 3

Doubts

In the result of the comparison:

6 + 0 + 3 + 0 + 1 + 1 = 11

Where does 0 come from? How is the comparison made?

For now, I have only the signature of the method, which would look this way:

public static Integer semelhancaString(Integer n, String... strings) {}
  • 1

    Good. The site is for code correction and code analysis. It tries to create a code that we analyze it. But when comparing occurs with the string "aa" an error occurs. Pois

2 answers

0

Instead of spending a lot of String, is easier if you pass as parameter a Array or List of String.

And as a return it is not possible to create a return that has T Lines of the kind Interger, the only way is to return a Array of Integer or by String

A breeding alternative is this one I rode below

String[] s1 = {"ababaa", "babaa", "abaa", "baa", "aa", "a"};
String[] s2 = {"ababaa", "aa"};

private static String semelhancaString(int n, String[] s1, String[] s2) {
            StringBuilder saida = new StringBuilder();

            for (int i = 0; i < n; i++) {
                int total = 0;
                for (int j = 0; j < s1.length; j++) {
                    String palavra1 = s1[j];
                    String palavra2 = s2[i];

                    int index = 0;
                    int nComparações = 0;
                    while (index < palavra1.length() && index < palavra2.length()) {
                        if (palavra1.charAt(index) == palavra2.charAt(index)) {
                            index++;
                            nComparações++;
                        } else {
                            break;
                        }
                    }
                    total += nComparações;
                }
                saida.append(total + "\n");

            }
            return saida.toString();
        }

0


I managed to solve it, so making the code cleaner:

public static void semelhancaString(Integer n, String... strings) {
List<String> listStrings;
String stringBase, stringComparator;
Integer quantidade = 0;

listStrings = new ArrayList<String>(Arrays.asList(strings));
stringBase = listStrings.get(0);

for (int j = 1; j < n; j++) {
    stringComparator = listStrings.get(j);
    quantidade = contaQuantidadeDeIgualdade(stringBase, stringComparator);
    System.out.println(
        "A suma das Semelhanças entre: " + stringBase + " e " + stringComparator + " é " + quantidade);
}
}

public static Integer contaQuantidadeDeIgualdade(String base, String comparator) {
Integer quantidade = 0;
for (int i = base.length(); i > 0; i--) {
    String substring = base.substring(0, i);
    if (comparator.contains(substring)) {
    quantidade++;
    }

}
return quantidade;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.