【问题标题】:How to determine if a string was only edited once in java?java - 如何确定一个字符串是否只在java中编辑过一次?
【发布时间】:2021-07-22 17:03:01
【问题描述】:

我正在尝试确定一个字符串是否只编辑过一次。当我输入以下字符串(见下面的代码)时,输出不正确。程序应该打印出字符串只编辑过一次,这不是我在屏幕上看到的。谁能帮帮我?

public static void main(String[] args) {
        String str1 = "cat";
        String str2 = "ca";
        char[] arr1 = str1.toCharArray();
        char[] arr2 = str2.toCharArray();
        int n = str1.length();
        int m = str2.length();
        if (stringWasEdited(str1, str2, arr1, arr2, m, n)) {
            System.out.print("The string was edited only once.");
        } else {
            System.out.print("The string was not edited or was edited more than once.");
        }
    }

    private static boolean stringWasEdited(String str1, String str2, char[] arr1, char[] arr2, int m, int n) {
        int count = 0;
        if (Math.abs(m - n) > 1) {
            return false;
        }
        for (int i = 0; i < m; i++) {
            if (arr1[i] != arr2[i]) {
                count++;
            }
            if (m > n || n > m) {
                count++;
            }
        }
        if (count > 1 || count == 0) {
            return false;
        }
        return true;
    }
}

【问题讨论】:

标签: java arrays string edit


【解决方案1】:

你的问题是你只循环一个字符串的长度,而你应该从 i

static boolean isEditDistanceOne(String s1,
                                 String s2)
{
    // Find lengths of given strings
    int m = s1.length(), n = s2.length();
 
    // If difference between lengths is
    // more than 1, then strings can't
    // be at one distance
    if (Math.abs(m - n) > 1)
        return false;
 
    int count = 0; // Count of edits
 
    int i = 0, j = 0;
    while (i < m && j < n)
    {
        // If current characters don't match
        if (s1.charAt(i) != s2.charAt(j))
        {
            if (count == 1)
                return false;
 
            // If length of one string is
            // more, then only possible edit
            // is to remove a character
            if (m > n)
                i++;
            else if (m< n)
                j++;
            else // Iflengths of both strings
                // is same
            {
                i++;
                j++;
            }
             
            // Increment count of edits
            count++;
        }
 
        else // If current characters match
        {
            i++;
            j++;
        }
    }
 
    // If last character is extra
    // in any string
    if (i < m || j < n)
        count++;
 
    return count == 1;
}

参考链接:https://www.geeksforgeeks.org/check-if-two-given-strings-are-at-edit-distance-one/

【讨论】:

    【解决方案2】:

    我假设您想检查一个编辑,拒绝包含零个或多个的字符串。

    给定两个字符串 (s1, s2),其中 s1 的长度大于或等于 s2(如果不是这种情况,请反转参数)我们可以使用以下命令检查单个编辑:

    1. 过滤掉长度差大于1的情况。

    2. 使用第一个相等字符的序列。

    3. 如果您已到达s2 的末尾,请检查s1 是否更长。如果不是,则s1 == s2 不满足条件。

    4. 使用第二个相等字符序列。如果s1 更长(加一),那么我们在访问s2 中的字符时需要使用偏移量1。

    5. 如果您已到达s1 的末尾,则满足条件。

    以上翻译成Java(Ideone):

    static boolean hasOneEdit(String s1, String s2)
    {
        // ensure s1 is longer than or equal to s2
        if(s1.length() < s2.length()) return hasOneEdit(s2, s1);
        
        // difference in length cannot be more than one
        if(s1.length() - s2.length() > 1) return false;
    
        int pos = 0;
        
        // consume 1st sequence of equal characters
        for(; pos < s2.length() && s1.charAt(pos) == s2.charAt(pos); pos++); 
        
        // if we reached the end of s2, s1 has to be longer (by 1)
        if(pos == s2.length()) return pos < s1.length();
    
        // offset into s2 depends on whether strings are equal in length
        int off = s1.length() - s2.length();
                
        // consume 2nd sequence of equal characters
        for(pos += 1; pos < s1.length() && s1.charAt(pos) == s2.charAt(pos-off); pos++);
        
        // did we reach the end of s1?
        return pos == s1.length();
    }
    

    还有一个小测试程序:

    public static void main(String[] args)
    {
        String s1 = "dirigible";
        for(int i=0; i<s1.length(); i++)
        {
            String s2 = s1.substring(0, i) + s1.substring(i+1);
            test(s1, s2, true);
            for(int j=0; j<s2.length(); j++)
            {
                String s3 = s2.substring(0, j) + "x" + s2.substring(j+1);
                test(s1, s3, false);
            }               
        }
        for(int i=0; i<s1.length(); i++)
        {
            String s2 = s1.substring(0, i) + "x" + s1.substring(i+1);
            test(s1, s2, true);
            for(int j=0; j<s2.length(); j++)
            {               
                String s3 = s2.substring(0, j) + s2.substring(j+1);
                test(s1, s3, j == i);
            }
        }
    }
    
    static void test(String s1, String s2, boolean check)
    {
        boolean result = hasOneEdit(s1, s2);
        System.out.println((result == check ? "PASS" : "FAIL") + ": " + s1 + " " + s2 + " = " + check);
    }
    

    输出:

    PASS: dirigible irigible = true
    PASS: dirigible xrigible = false
    PASS: dirigible ixigible = false
    PASS: dirigible irxgible = false
    PASS: dirigible irixible = false
    <Truncated>
    PASS: dirigible dirgiblx = false
    PASS: dirigible diriiblx = false
    PASS: dirigible dirigblx = false
    PASS: dirigible dirigilx = false
    PASS: dirigible dirigibx = false
    PASS: dirigible dirigibl = true
    

    【讨论】:

      猜你喜欢
      • 2013-11-26
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 2012-12-26
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      相关资源
      最近更新 更多