Skip to main content

MAY-18 2020 Challenge

MAY-18 2020 Challenge : Permutation in String



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 in C++: 

class Solution {

    public boolean checkInclusion(String s1, String s2) {

        int[] map = new int[128];

        for (char c : s1.toCharArray()) {

            map[c]++;

        }

        int count = s1.length();

        char[] chars = s2.toCharArray();

        int left = 0, right = 0;

        while (right < chars.length) {

            if (map[chars[right++]]-- > 0) count--;

            while (count == 0) {

                if (right - left == s1.length()) return true;

                if (++map[chars[left++]] > 0) count++;

            }

        }

        return false;

    }

}

Permutation in String

Comments

Popular posts from this blog

Leetcode 190. Reverse Bits. Python. Bit Manipulation

  190 .  Reverse Bits Reverse bits of a given 32 bits unsigned integer. Note: Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned. In Java, the compiler represents the signed integers using  2's complement notation . Therefore, in  Example 2  above, the input represents the signed integer  -3  and the output represents the signed integer  -1073741825 .   Example 1: Input: n = 00000010100101000001111010011100 Output: 964176192 (00111001011110000010100101000000) Explanation: The input binary string 00000010100101000001111010011100 represents the unsigned integer 43261596, so return 964176192 which its binary representation is 00111001011110000010100101000000 . Example 2: Input: n = 11111111111111111111...

Leetcode 417. Pacific Atlantic Water Flow. Python (Dfs Graph)

417 .  Pacific Atlantic Water Flow There is an  m x n  rectangular island that borders both the  Pacific Ocean  and  Atlantic Ocean . The  Pacific Ocean  touches the island's left and top edges, and the  Atlantic Ocean  touches the island's right and bottom edges. The island is partitioned into a grid of square cells. You are given an  m x n  integer matrix  heights  where  heights[r][c]  represents the  height above sea level  of the cell at coordinate  (r, c) . The island receives a lot of rain, and the rain water can flow to neighboring cells directly north, south, east, and west if the neighboring cell's height is  less than or equal to  the current cell's height. Water can flow from any cell adjacent to an ocean into the ocean. Return  a  2D list  of grid coordinates  result  where  result[i] = [r i , c i ]  denotes that rain water can flow fro...

Leetcode 271. Encode and Decode Strings. Python (Easy)

Encode and Decode Strings Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings. Please implement  encode  and  decode Example Example1 Input: [ "lint" , "code" , "love" , "you" ] Output: [ "lint" , "code" , "love" , "you" ] Explanation: One possible encode method is: "lint:;code:;love:;you" Example2 Input: [ "we" , "say" , ":" , "yes" ] Output: [ "we" , "say" , ":" , "yes" ] Explanation: One possible encode method is: "we:;say:;:::;yes" Solution : class Solution:     def encode( self , strs):         ans = " "         for s in strs:             ans += str ( len (s)) + "$" + s         return ans     def decode( self , str ):         ans = []         i = 0                 w...