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.
ExampleExample 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
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)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/69721.html
摘要:從末位向前遍歷,假設(shè)循環(huán)開始全是倒序排列,如當(dāng)?shù)谝淮纬霈F(xiàn)正序的時候,如的和此時從數(shù)列末尾向前循環(huán)到,找到第一個比大的交換這兩個數(shù),變成倒置第位到末位的數(shù)為正序排列這里的是完全倒置的排列,如,即上面循環(huán)的情況完全沒有出現(xiàn), Problem Implement next permutation, which rearranges numbers into the lexicographic...
摘要:我覺得雖然在里分類是,但其實是一道難題。思路如下搞一個哈希表,存儲數(shù)組中每一位的后面小于它的數(shù)的個數(shù)。與上一題的不同之處時會有重復(fù)的數(shù)。當(dāng)然,每個重復(fù)數(shù)的都要階乘,例如有個,個,就是。是所有排列的次數(shù)和,返回下一次。 Permutation Index Problem Given a permutation which contains no repeated number, find...
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...
摘要:做法先把這個數(shù)放入一個數(shù)組里,同時計算出的階乘。假設(shè)這一組是第組,第一個數(shù)就是,同時刪去這個數(shù),并讓除以取余作為新的。如此循環(huán),這樣,下一組的成員數(shù)減少了,要找的位置也更為精確了。 Problem Given n and k, return the k-th permutation sequence. Example For n = 3, all permutations are li...
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...
閱讀 3268·2023-04-25 22:47
閱讀 3778·2021-10-11 10:59
閱讀 2313·2021-09-07 10:12
閱讀 4269·2021-08-11 11:15
閱讀 3440·2019-08-30 13:15
閱讀 1757·2019-08-30 13:00
閱讀 976·2019-08-29 14:02
閱讀 1691·2019-08-26 13:57