成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LeetCode] 567. Permutation in String

geekzhou / 3126人閱讀

Problem

Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. In other words, one of the first string"s permutations is the substring of the second string.
Example 1:

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

Example 2:

Input:s1= "ab" s2 = "eidboaoo"
Output: False

Note:

The input strings only contain lower case letters.
The length of both given strings is in range [1, 10,000].

Solution
class Solution {
    public boolean checkInclusion(String s1, String s2) {
        int l1 = s1.length(), l2 = s2.length();
        if (l2 < l1) return false;
        
        Map map = new HashMap<>();
        for (int i = 0; i < l1; i++) {
            map.put(s1.charAt(i), map.getOrDefault(s1.charAt(i), 0)+1);
            map.put(s2.charAt(i), map.getOrDefault(s2.charAt(i), 0)-1);
        }
        if (checkMap(map)) return true;
        for (int i = l1; i < l2; i++) {
            map.put(s2.charAt(i), map.getOrDefault(s2.charAt(i), 0)-1);
            map.put(s2.charAt(i-l1), map.get(s2.charAt(i-l1))+1);
            if (checkMap(map)) return true;
        }
        return false;
    }
    private boolean checkMap(Map map) {
        for (Map.Entry entry: map.entrySet()) {
            if (entry.getValue() != 0) return false;
        }
        return true;
    }
}
using int array as map
class Solution {
    public boolean checkInclusion(String s1, String s2) {
        if (s1 == null || s2 == null || s1.length() > s2.length()) return false;
        int[] count = new int[26];
        for (int i = 0; i < s1.length(); i++) {
            count[s1.charAt(i)-"a"]++;
            count[s2.charAt(i)-"a"]--;
        }
        if (allZero(count)) return true;
        for (int i = s1.length(); i < s2.length(); i++) {
            count[s2.charAt(i)-"a"]--;
            count[s2.charAt(i-s1.length())-"a"]++;
            if (allZero(count)) return true;
        }
        return false;
    }
    
    private boolean allZero(int[] arr) {
        for (int i: arr) {
            if (i != 0) return false;
        }
        return true;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/71904.html

相關(guān)文章

  • [Leetcode] Permutation Sequence 全排列序列

    摘要:找規(guī)律復(fù)雜度時(shí)間空間思路由于我們只要得到第個(gè)全排列,而不是所有全排列,我們不一定要將所有可能都搜索一遍。根據(jù)全排列順序的性質(zhì),我們可以總結(jié)出一個(gè)規(guī)律假設(shè)全排列有個(gè)數(shù)組成,則第個(gè)全排列的第一位是。然后將得到,這個(gè)就是下一輪的。 Permutation Sequence The set [1,2,3,…,n] contains a total of n! unique permutati...

    testHs 評(píng)論0 收藏0
  • leetcode60. Permutation Sequence

    摘要:題目要求假設(shè)按照題中給的排列組合的順序,假設(shè)有個(gè)數(shù)字,返回第個(gè)排列組合的結(jié)果。最后在個(gè)位上,選擇中的第一個(gè)。這時(shí)知道以第位為開頭的結(jié)果值有此時(shí)第個(gè)結(jié)果集在該位上的選擇為。依次往后類推,直至到最后一位。 題目要求 The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling...

    xiaokai 評(píng)論0 收藏0
  • [Leetcode] Palindrome Permutation 回文變換

    摘要:最笨的方法就是用的解法,找出所有的,然后再用中判斷回文的方法來判斷結(jié)果中是否有回文。而中心對(duì)稱點(diǎn)如果是字符,該字符會(huì)是奇數(shù)次,如果在兩個(gè)字符之間,則所有字符都是出現(xiàn)偶數(shù)次。 Palindrome Permutation Given a string, determine if a permutation of the string could form a palindrome. F...

    svtter 評(píng)論0 收藏0
  • [Leetcode] Next Permutation 下一個(gè)排列

    摘要:因?yàn)樵黾痈呶粫?huì)帶來更大的增益。所以對(duì)于一個(gè)長(zhǎng)為的序列,我們?cè)黾拥谖坏那疤崾?,前位已?jīng)達(dá)到了最大排列方法。因?yàn)槭钦蚁乱粋€(gè)數(shù),所以我們要找一個(gè)比小卻盡可能大的數(shù),所以找到。把換到的位置后,后三位仍然是個(gè)降序的排列。 Next Permutation Implement next permutation, which rearranges numbers into the lexicogr...

    young.li 評(píng)論0 收藏0
  • [LeetCode] 267. Palindrome Permutation II

    Problem Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. Example Given s = aabb, return [abba,baa...

    huashiou 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<