【问题标题】:Wrong Answer : Permutation in String : fix error(Leetcode)错误答案:字符串中的排列:修复错误(Leetcode)
【发布时间】:2021-01-23 23:56:47
【问题描述】:

给定两个字符串 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;
    }
}

提前致谢:)

【问题讨论】:

  • 感谢您诚实地要求为您解决问题。这样做的目的是让您思考并尝试自己解决。
  • 一个建议:添加一个“调试”日志以打印您在每次迭代中比较的内容,这应该对您有所帮助

标签: java data-structures sliding-window


【解决方案1】:

存在算法问题,给出错误答案(不是效率问题)。这也可以通过:

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/memory 分析@ 987654325@, 2.

【讨论】:

    【解决方案2】:

    我认为在您的情况下,时间限制已超出。 请同样确认。 你可以尝试使用 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多