英文:
Check if it is possible to transform one string to another using recursion
问题
我正在尝试使用递归解决以下方法:
https://www.geeksforgeeks.org/check-possible-transform-one-string-another/
static boolean abbreviation(String a, String b, int m, int n) {
char[] x = a.toCharArray();
if (m == 0 || n == 0)
return true;
if (m == 0 && n != 0)
return false;
if (n == 0) {
for (int i = 0; i < x.length; i++) {
// 如果任何字符不是小写,返回false
if (Character.isLowerCase(x[i])) {
System.out.println(x[i]);
return true;
}
}
return false;
}
if ((a.charAt(m - 1) == b.charAt(n - 1)) ||
(Character.toUpperCase(a.charAt(m - 1)) == b.charAt(n - 1))) {
return abbreviation(a, b, m - 1, n - 1);
} else
return abbreviation(a, b, m - 1, n) && abbreviation(a, b, m, n - 1);
}
我对以下输入得到了true
,而不是false
:
输入 1
AbCdE
AFE
输入 2
beFgH
EFG
<details>
<summary>英文:</summary>
I am trying to solve the below method by using recursion
https://www.geeksforgeeks.org/check-possible-transform-one-string-another/
static boolean abbreviation(String a, String b,int m, int n) {
char[] x = a.toCharArray();
if(m ==0|| n==0)
return true;
if(m==0 && n!=0)
return false;
if(n==0){
for(int i=0; i < x.length; i++){
//if any character is not in lower case, return false
if( Character.isLowerCase( x[i] )){
System.out.println(x[i]);
return true;
}
}
return false;
}
if((a.charAt(m-1)==b.charAt(n-1))||
(Character.toUpperCase(a.charAt(m-1))==b.charAt(n-1))){
return abbreviation(a,b,m-1,n-1);
}else return abbreviation(a,b,m-1,n) && abbreviation(a,b,m,n-1);
}
I am getting`true` instead of `false` for below inputs
**Input 1**
AbCdE
AFE
**Input 2**
beFgH
EFG
</details>
# 答案1
**得分**: 1
以下是翻译好的内容:
当遍历两个字符串的字符时,有三种情况:
- 字符匹配(如果是小写,则转为大写)
- 字符不匹配且第一个字符串的字符为小写
- 字符不匹配且第一个字符串的字符为大写
基本情况是,如果第一个字符串中的所有字符都已检查完毕,则检查是否全部匹配。
```java
boolean abbreviation(String a, String b, int m, int n) {
if (m == 0)
return n == 0;
if (n > 0 && Character.toUpperCase(a.charAt(m - 1)) == b.charAt(n - 1)) {
return abbreviation(a, b, m - 1, n - 1);
} else if (!Character.isUpperCase(a.charAt(m - 1))) {
return abbreviation(a, b, m - 1, n);
} else {
return false;
}
}
演示:
System.out.println(abbreviation("AbCdE", "AFE", 5, 3));
System.out.println(abbreviation("beFgH", "EFG", 5, 3));
System.out.println(abbreviation("beFgh", "EFG", 5, 3));
System.out.println(abbreviation("daBcd", "ABC", 5, 3));
输出:
false
false
true
true
英文:
You have 3 cases when traversing characters of two string
- Character matched (if lowercase then doing uppercase)
- Charcter not matched and first string character is lowercase
- Charcter not matched and first string character is uppercase
And base case if all character checked in first string then check all matched or not.
boolean abbreviation(String a, String b, int m, int n) {
if (m == 0)
return n == 0;
if (n > 0 && Character.toUpperCase(a.charAt(m - 1)) == b.charAt(n - 1)) {
return abbreviation(a, b, m - 1, n - 1);
} else if(!Character.isUpperCase(a.charAt(m - 1))) {
return abbreviation(a, b, m - 1, n);
} else {
return false;
}
}
Demo:
System.out.println(abbreviation("AbCdE", "AFE", 5, 3));
System.out.println(abbreviation("beFgH", "EFG", 5, 3));
System.out.println(abbreviation("beFgh", "EFG", 5, 3));
System.out.println(abbreviation("daBcd", "ABC", 5, 3));
Output:
false
false
true
true
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论