检查是否可以使用递归将一个字符串转换成另一个字符串。

huangapple go评论83阅读模式
英文:

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 &amp;&amp; n!=0)
        return false;
    
        if(n==0){
            for(int i=0; i &lt; 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) &amp;&amp;  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 &gt; 0 &amp;&amp; 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(&quot;AbCdE&quot;, &quot;AFE&quot;, 5, 3));
System.out.println(abbreviation(&quot;beFgH&quot;, &quot;EFG&quot;, 5, 3));
System.out.println(abbreviation(&quot;beFgh&quot;, &quot;EFG&quot;, 5, 3));
System.out.println(abbreviation(&quot;daBcd&quot;, &quot;ABC&quot;, 5, 3));

Output:

false
false
true
true

huangapple
  • 本文由 发表于 2020年10月10日 23:04:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64294874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定