Tuesday, February 19, 2019

Permutations in String:
Given two strings s1 and s2, , function to return true, if S2, containts the permutations of S1.
Example 1:
Input:s1 = "abc" s2 = "eidcbaooo"
Output:True
Explanation: s2 contains one permutation of s1 ("cba").
Leet Code: https://leetcode.com/problems/permutation-in-string/
GeeksForGeeks Similar problem:
https://www.geeksforgeeks.org/check-if-two-strings-are-permutation-of-each-other/
Hints:
1) Obviously, brute force will result in TLE. Think of something else.
2) how will you check one string is permutation of another??(sort both strings and check if all of the both strings match??)
3) If one string is a permutation of another string then they must one common metric. What is that? (Character frequencies)
4) Both strings must have same character frequencies, if one is permutation of another. Which data structure should be used to store frequencies?(int[26])
5) What about hash table? An array of size 26?





No comments:

Post a Comment