Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1bdec39

Browse files
HackerEarth String Problem
1 parent a45d359 commit 1bdec39

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Xenny had a list of N strings of equal length. He wanted to sort them by the first M characters only. That
2+
# means, while sorting the list of strings, he only wanted to consider the first M characters of each string.
3+
# Help Xenny to find out the Kth string in the list after he sorts them.
4+
#
5+
# Note: Xenny wanted to perform stable sorting.
6+
# Stable sorting algorithms maintain the relative order of records with equal keys (i.e. values). That is, a
7+
# sorting algorithm is stable if whenever there are two records R and S with the same key and with R
8+
# appearing before S in the original list, R will appear before S in the sorted list.
9+
#
10+
# Input
11+
#
12+
# First line contains a single integer - T, which represents the number of testcases.
13+
# T testcases follow.
14+
# Each testcase is of the following format:
15+
# First line contains 3 space-separated integers - N, K and M.
16+
# N is the total number of strings Xenny has.
17+
# K is the index of the string in the list after sorting, which Xenny has to find.
18+
# M is the number of characters based on which sorting will be done by Xenny.
19+
# Then next N lines contain N strings ( each line will contain one string ) .
20+
#
21+
# Output
22+
#
23+
# For each testcase, print the Kth string in the sorted list in a new line.
24+
#
25+
# Constraints
26+
#
27+
# 1 ≤ T ≤ 50
28+
# 1 ≤ N ≤ 103
29+
# 1 ≤ Max Length of each String ≤ 103
30+
# 1 ≤ M ≤ Max Length
31+
# M ≤ Max Length of each String ≤ 103
32+
#
33+
# SAMPLE INPUT
34+
# 1
35+
# 3 1 3
36+
# abcdef
37+
# abcaaa
38+
# aabaaa
39+
#
40+
# SAMPLE OUTPUT
41+
# aabaaa
42+
43+
for _ in range(int(input())):
44+
n, k, m = input().split()
45+
strings = []
46+
for i in range(int(n)):
47+
strings.append(input())
48+
49+
array = sorted(strings, key = lambda x: x[:int(m)])
50+
print(array[int(k) - 1])

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /