微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

错误答案:字符串排列:修复错误Leetcode

如何解决错误答案:字符串排列:修复错误Leetcode

给出两个字符串s1和s2,编写一个函数,如果s2包含s1的排列,则返回true。换句话说,第一个字符串的排列之一是第二个字符串的子字符串。

Example 1: (Test Case Passed)

Input: s1 = "ab" s2 = "eidbaooo"
Output: True
Explanation: s2 contains one permutation of s1 ("ba").

Example 2: (Test Case Failed)

Input: s1 = "adc",s2 = "dcda"
Output: True
Expected : False

该问题在leetcode上可用:https://leetcode.com/problems/permutation-in-string/submissions/

我已经通过了78/103个测试用例。我在使用我认为的条件时犯了一些错误,任何人都可以修复它。

这是我的代码

class Solution {
    public boolean checkInclusion(String s1,String s2) {
        int k = s1.length();
        HashMap<Character,Integer> map = new HashMap<>();
        for(int i=0; i<k; i++){
            char rightChar = s1.charat(i);
            map.put(rightChar,map.getorDefault(rightChar,0)+1);
        }
        int windowStart=0;
        int decrement=0;
        HashMap<Character,Integer> resMap = new HashMap<>();
        for(int windowEnd=0; windowEnd<s2.length(); windowEnd++){
            char nextChar = s2.charat(windowEnd);
           resMap.put(nextChar,resMap.getorDefault(nextChar,0)+1);
            
            if(windowEnd-windowStart+1 >= k){
                if(resMap.equals(map)){
                    return true;
                }else{         
                    char leftChar = s2.charat(windowStart);
                     resMap.remove(leftChar);  
                     windowStart++;
                }
            }
        }
        
        return false;
    }
}

预先感谢:)

解决方法

我认为您的时间限制已超出。 请确认相同。 您可以使用Leetcode提供的Array尝试解决方案-

public boolean checkInclusion(String s1,String s2){

    if (s1.length() > s2.length())
        return false;
    int[] s1map = new int[26];
    for (int i = 0; i < s1.length(); i++)
        s1map[s1.charAt(i) - 'a']++;
    for (int i = 0; i <= s2.length() - s1.length(); i++) {
        int[] s2map = new int[26];
        for (int j = 0; j < s1.length(); j++) {
            s2map[s2.charAt(i + j) - 'a']++;
        }
        if (matches(s1map,s2map))
            return true;
    }
    return false;
}


public boolean matches(int[] s1map,int[] s2map) {
    for (int i = 0; i < 26; i++) {
        if (s1map[i] != s2map[i])
            return false;
    }
    return true;
}
,

有一个算法问题,给出了错误的答案(不是效率问题)。这也可以通过:

public class Solution {
    public final boolean checkInclusion(
        final String a,final String b
    ) {
        if (a.length() > b.length()) {
            return false;
        }

        int[] countmap = new int[26];

        for (int index = 0; index < a.length(); index++) {
            countmap[a.charAt(index) - 'a']++;
            countmap[b.charAt(index) - 'a']--;
        }

        if (isAllZero(countmap)) {
            return true;
        }

        for (int index = a.length(); index < b.length(); index++) {
            countmap[b.charAt(index) - 'a']--;
            countmap[b.charAt(index - a.length()) - 'a']++;

            if (isAllZero(countmap)) {
                return true;
            }
        }

        return false;
    }

    private static final boolean isAllZero(
        final int[] counts
    ) {
        for (int alphabet = 0; alphabet < 26; alphabet++)
            if (counts[alphabet] != 0) {
                return false;
            }

        return true;
    }

}

参考文献

  • 有关其他详细信息,请参见Discussion Board,在这里您可以找到许多具有各种languages且已被广泛接受的解决方案,包括低复杂度算法和渐近runtime / {{ 3}}分析memory1

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。