Find string matching position?

I have a string like: PQqRrp. An uppercase letter followed by a lowercase letter.

If the Upper is followed by its lowercase letter, then there is a coincidence. Therefore, I need to go to the moment when there will be the last match of the upper and lower letters. If the bottom corresponds to its lowest, then this does not correspond.

Now I need to find the last position in which my character coincided (upper with lower alphabet). In the above case in 5th place r was matching with R.

private int getStringMatchNumber(String input) {
    char[] str = input.toLowerCase().toCharArray();
    // now I am confuse
}

How am I supposed to go here?

+4
source share
3 answers

You can start at the end of the line and go back in pseudocode:

char[] str = input.toCharArray()
for i = str.length-1 down to 1:
   if str[i] is lowercase:
      if toLowerCase(str[i-1]) == str[i] and str[i-1] is uppercase:
         return i-1

return -1  // means no match is found

Java Code:

    char[] str = input.toCharArray();

    for(int i=str.length-1; i>=1; --i) {
        char c = str[i];
        char p = str[i-1];
        if (c == Character.toLowerCase(c) && p == Character.toUpperCase(p)){
            if (Character.toLowerCase(p) == c)
                return i-1;
        }
    }
    return -1;
+2
source

( ) (, A a ABba), 3, 2, A a)

:

private static int getStringMatchNumber(String input) {
    char[] str = input.toCharArray();

    int last = findMatches(str, 0);
    System.out.println("Last unmatched index: " + (last - 1));

    for(int i = str.length-1; i > 0; i--) {
        Character c = str[i];
        Character p = str[i-1];
        if (Character.isLowerCase(c) && Character.isUpperCase(p)){
            if (p == Character.toUpperCase(c))
                return i;
        }
    }
    return 0;
}

private static int findMatches(char[] str, int pos) {

    if(pos+1 >= str.length) {
        return pos;
    }
    Character c = str[pos];
    Character p = str[pos+1];
    int nextPos = 0;

    System.out.println(c + " " + p);

    if (Character.isUpperCase(c) && Character.isLowerCase(p)){
         if (c == Character.toUpperCase(p)) {
             System.out.println("Match found");
             pos = pos+2;
             pos = findMatches(str, pos);
         } else {
             System.out.println("Next lower case letter doesn't match. Stop.");
         }
    } else if(Character.isUpperCase(c) && Character.isUpperCase(p)) {
        System.out.println("Trying to find inner Matches first");

        nextPos = findMatches(str, pos+1);
        if(nextPos < str.length) {
            p = str[nextPos];
            System.out.println("Outer match character: " + p);
            System.out.println(c + " " + p);

            if ((Character.isUpperCase(c) && Character.isLowerCase(p)) && (c == Character.toUpperCase(p))){
                System.out.println("Outer Match found");
                pos = nextPos + 1;
            } else {
                pos = nextPos;
            }
        }
        if(pos < str.length) {
            pos = findMatches(str, pos);
        }
    } else {
        System.out.println("Lower case letter found, will return and find outer match");
    }

    return pos;
}
+1

, . , , , , . , .

for , . , i. , . if-conditional, .

private static int getStringMatchNumber(String input) {
    char[] str = input.toCharArray();
    for(int i = str.length-1; i > 0; i--) {
        Character c = str[i];
        Character p = str[i-1];
        if (Character.isLowerCase(c) && Character.isUpperCase(p)){
            if (p == Character.toUpperCase(c))
                return i;
        }
    }
    return 0;
}

, 0, , . .

EDIT I performed a check for lowercase and uppercase letters in the corresponding elements (as suggested by others). Note that the return value is the value of the zero-basedposition of the bottom character in the match. For the input value, the "aaaBbaa"return value 4i.e. position bin coincidence Bb. You can adjust the algorithm to return the position bby returning i-1instead i. In this case, you need to return -1instead 0to avoid false negatives.

0
source

Source: https://habr.com/ru/post/1589537/


All Articles