Answers to: Help in Maximum Permutation(Hackerrank University CodeSprint 4)https://discuss.codechef.com/questions/123668/help-in-maximum-permutationhackerrank-university-codesprint-4<pre><code>from collections import Counter
t = int(input())
while t:
s1 = input()
n = len(s1)
s2 = input()
m = len(s2)
l = []
d = {i: 0 for i in "qwertyuiopasdfghjklzxcvbnm"}
td = {i: 0 for i in "qwertyuiopasdfghjklzxcvbnm"}
for i in range(n):
d[s1[i]] += 1
td[s2[i]] += 1
if td == d:
l.append(s2[:n])
for i in range(1, m - n):
td[s2[i - 1]] -= 1
td[s2[i + n - 1]] += 1
if td == d:
l.append(s2[i:i + n])
l = Counter(l).most_common()
if len(l) == 0:
print(-1)
else:
ans = l[0][0]
c = l[0][1]
for i in l:
if i[1] > c:
ans = i[0]
c = i[1]
elif i[1] == c:
ans = min(ans, i[0])
print(ans)
t -= 1
</code></pre>
<p>This is my code in python-3 for <a href="https://www.hackerrank.com/contests/university-codesprint-4/challenges/maximum-permutation/problem">Maximum Permutation</a> of <a href="https://www.hackerrank.com/contests/university-codesprint-4/challenges">Hackerrank University Codesprint 4</a></p>
<p>I'm getting runtime error for few cases. Could anyone please help me find what's the mistake in above code which causes runtime error.</p>
<p><strong><em>My approach</em></strong><br>
- I've made a dictionary(<strong>d</strong>) which stores count of all characters in string s1 (smaller string)<br>
- I've also made a dictionary(<strong>td</strong>) which stores count of all characters in substring of s2[0, n) where n is length of s1<br>
- Now, if both the dictionaries are equal then I've pushed the substring in a list l<br>
- Repeating this for all other substrings I've increased and decreased the count of charcters I add and remove (in td) and if the dictionaries are equal then I've pushed the substring in a list l<br>
- Then the list of substrings is converted to list of tuples in which each tuple stores 2 values (string, frequency)<br>
- Then I find the most frequently occuring substring (lexicographically smallest if there are multiple substrings occuring same number of times)<br></p>enMon, 26 Feb 2018 08:23:38 +0530Answer by vivek_1998299https://discuss.codechef.com/questions/123668/help-in-maximum-permutationhackerrank-university-codesprint-4/123674<p>Probably because u are trying to allocate too much memory(O(n^2)).</p>
<p>Consider the situation where both strings are equal.</p>
<p>Just out of curiousity,how much points did this fetch(because python can do anything)?</p>vivek_1998299Mon, 26 Feb 2018 08:23:38 +0530https://discuss.codechef.com/questions/123668/help-in-maximum-permutationhackerrank-university-codesprint-4/123674