查找字符串匹配位置?
Find string matching position?
我有一个这样的字符串:PQqRrp
。大写字母后跟小写字母。
如果 Upper 后跟它自己的小写字母,则匹配。所以我需要走到大写字母和小写字母最后一场比赛的地步。如果 lower 与它自己的 lower 匹配,那么这不是匹配项。
现在我需要找到我的角色匹配的最后一个位置(大写字母与其小写字母)。在上面的例子中,在第 5 位,r was matching with R
.
private int getStringMatchNumber(String input) {
char[] str = input.toLowerCase().toCharArray();
// now I am confuse
}
我应该如何进行这里操作?
您应该继续编写程序的伪代码。
从您发布的代码来看,据我所知,您不需要将输入转换为小写。这会将所有内容更改为小写并且您不能使用这些值。
我会 运行 从字符数组的末尾开始 for
循环,并检查每对连续的值是否相等。由于循环从末尾 运行s 开始,匹配的第一个实例将 return 值 i
。当您正在寻找最后一场比赛时,向后的方法是有效的。该方法也只使用一个 if 条件,因此在长 运行.
中占用的内存更少
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;
}
请注意,如果函数 returns 为 0,则表示未找到匹配对。您可以在被叫方之后查看。
EDIT 我在匹配项中实施了小写和大写检查(正如其他人所建议的)。请注意,returned 值是匹配中小写字符位置的 zero-based
值。对于输入值 "aaaBbaa"
,returned 值是 4
,即 b
在 Bb
匹配中的位置。您可以通过 returning i-1
而不是 i
将算法调整到 return B
的位置。在这种情况下,您需要 return -1
而不是 0
以避免漏报。
你可以从字符串的末尾开始向后走,在伪代码中:
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代码:
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;
其他答案(包括已接受的答案)不考虑外部匹配(例如 ABba 中的 A - 应该 return 3,但其他解决方案 return 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;
}
我有一个这样的字符串:PQqRrp
。大写字母后跟小写字母。
如果 Upper 后跟它自己的小写字母,则匹配。所以我需要走到大写字母和小写字母最后一场比赛的地步。如果 lower 与它自己的 lower 匹配,那么这不是匹配项。
现在我需要找到我的角色匹配的最后一个位置(大写字母与其小写字母)。在上面的例子中,在第 5 位,r was matching with R
.
private int getStringMatchNumber(String input) {
char[] str = input.toLowerCase().toCharArray();
// now I am confuse
}
我应该如何进行这里操作?
您应该继续编写程序的伪代码。 从您发布的代码来看,据我所知,您不需要将输入转换为小写。这会将所有内容更改为小写并且您不能使用这些值。
我会 运行 从字符数组的末尾开始 for
循环,并检查每对连续的值是否相等。由于循环从末尾 运行s 开始,匹配的第一个实例将 return 值 i
。当您正在寻找最后一场比赛时,向后的方法是有效的。该方法也只使用一个 if 条件,因此在长 运行.
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;
}
请注意,如果函数 returns 为 0,则表示未找到匹配对。您可以在被叫方之后查看。
EDIT 我在匹配项中实施了小写和大写检查(正如其他人所建议的)。请注意,returned 值是匹配中小写字符位置的 zero-based
值。对于输入值 "aaaBbaa"
,returned 值是 4
,即 b
在 Bb
匹配中的位置。您可以通过 returning i-1
而不是 i
将算法调整到 return B
的位置。在这种情况下,您需要 return -1
而不是 0
以避免漏报。
你可以从字符串的末尾开始向后走,在伪代码中:
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代码:
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;
其他答案(包括已接受的答案)不考虑外部匹配(例如 ABba 中的 A - 应该 return 3,但其他解决方案 return 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;
}