亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[LintCode] Permutation in String

wenshi11019 / 1250人閱讀

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

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

Solution
public class Solution {
    /**
     * @param s1: a string
     * @param s2: a string
     * @return: if s2 contains the permutation of s1
     */
    public boolean checkInclusion(String s1, String s2) {
        int len = s1.length();
        for (int i = 0; i <= s2.length()-len; i++) {
            if (isPermutation(s2.substring(i, i+len), s1)) return true;
        }
        return false;
    }
    
    //use the method of #String Permutation
    public boolean isPermutation(String s1, String s2) {
        if (s1 == null) return s2 == null;
        if (s2 == null) return s1 == null;
        if (s1.length() != s2.length()) return false;
        
        int[] dict = new int[256];
        char[] c1 = s1.toCharArray();
        char[] c2 = s2.toCharArray();
        for (char ch: c1) {
            dict[(int)ch]++;
        }
        for (char ch: c2) {
            dict[(int)ch]--;
            if (dict[(int)ch] < 0) return false;
        }

        return true;
    }
}

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

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

相關(guān)文章

  • [LintCode] Next Permutation II [Next Permutation]

    摘要:從末位向前遍歷,假設(shè)循環(huán)開始全是倒序排列,如當(dāng)?shù)谝淮纬霈F(xiàn)正序的時(shí)候,如的和此時(shí)從數(shù)列末尾向前循環(huán)到,找到第一個(gè)比大的交換這兩個(gè)數(shù),變成倒置第位到末位的數(shù)為正序排列這里的是完全倒置的排列,如,即上面循環(huán)的情況完全沒(méi)有出現(xiàn), Problem Implement next permutation, which rearranges numbers into the lexicographic...

    mikasa 評(píng)論0 收藏0
  • [LintCode] Permutation Index I & Permutation I

    摘要:我覺得雖然在里分類是,但其實(shí)是一道難題。思路如下搞一個(gè)哈希表,存儲(chǔ)數(shù)組中每一位的后面小于它的數(shù)的個(gè)數(shù)。與上一題的不同之處時(shí)會(huì)有重復(fù)的數(shù)。當(dāng)然,每個(gè)重復(fù)數(shù)的都要階乘,例如有個(gè),個(gè),就是。是所有排列的次數(shù)和,返回下一次。 Permutation Index Problem Given a permutation which contains no repeated number, find...

    lucas 評(píng)論0 收藏0
  • [LintCode] Previous Permutation

    Problem Given a list of integers, which denote a permutation. Find the previous permutation in ascending order. Notice The list may contains duplicate integers. Example For [1,3,2,3], the previous per...

    Pines_Cheng 評(píng)論0 收藏0
  • [LintCode] Permutation Sequence

    摘要:做法先把這個(gè)數(shù)放入一個(gè)數(shù)組里,同時(shí)計(jì)算出的階乘。假設(shè)這一組是第組,第一個(gè)數(shù)就是,同時(shí)刪去這個(gè)數(shù),并讓除以取余作為新的。如此循環(huán),這樣,下一組的成員數(shù)減少了,要找的位置也更為精確了。 Problem Given n and k, return the k-th permutation sequence. Example For n = 3, all permutations are li...

    Jacendfeng 評(píng)論0 收藏0
  • [LeetCode] 567. Permutation in String

    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 strings permutations is the substring of the second string.E...

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

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

0條評(píng)論

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