0

Consider this example of inverting the order of numbers from 0 to 10: x -> 10-x

This inverts the order in a bijective way. If I put in 100 numbers from 0 to 10 and it holds a < b for some a and b, then after applying the above formula, it holds a > b for all those a, b.


I need the same thing for lexicographic order. I have a string of the fixed length 10. How can I invert the lexicographic order?

It would be quite simple to do if it would only contain a-z. But it can also contain numbers and -, _ and it is case sensitive.

Does someone know how to do that?

mathematics-and-caffeine
  • 1,664
  • 2
  • 15
  • 19
  • If I understand what you're asking correctly, create another String with all the letters, numbers, and punctuation. Invert the order of your original string by finding the index of each character in the additional String and replacing each character with the character in the additional String at length - index. – Gilbert Le Blanc Dec 26 '21 at 10:47

1 Answers1

0
public static final String CHAR_INT_REPR_SPEICHERSTRING = "_-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

public static String invertiereString(String key) {
    String invertiert = "";

    for (int i = 0; i < key.length(); i++){
        String c = key.charAt(i) + "";
        invertiert += invertiereChar(c);
    }

    return invertiert;
}

public static String invertiereChar(String c) {
    int intRepr = gibCharIntRepr(c);

    // invertiere zahl
    int neueRepr = 64-intRepr-1;

    return gibChar_FromRepr_At(neueRepr);
}

public static int gibCharIntRepr(String c) {
    if (c.length() != 1) {
        return -1;
    }

    for (int i = 0; i < CHAR_INT_REPR_SPEICHERSTRING.length(); i++) {
        String cTemp = CHAR_INT_REPR_SPEICHERSTRING.charAt(i) + "";
        if (cTemp.equals(c)) {
            return i;
        }
    }

    return -1;
}

public static String gibChar_FromRepr_At(int at) {
    return CHAR_INT_REPR_SPEICHERSTRING.charAt(at)+"";
}

This does the job.

With a Hash-Map you can implement this much faster than with CHAR_INT_REPR_SPEICHERSTRING but since my Strings are no longer than 17 chars, I get a max of 17*64 iterations which is no problem.

mathematics-and-caffeine
  • 1,664
  • 2
  • 15
  • 19