根据给定的顺序对字符串数组进行排序 |第 2 组
给定一个字符串数组words[]和字母的顺序,任务是根据给定的顺序对数组进行排序。假设字典和单词只包含小写英文字母。
例子:
Input: words[] = {“hello”, “geeksforgeeks”}, order[] = “hlabcdefgijkmnopqrstuvwxyz”
Output: hello geeksforgeeks
Explanation: According to the given order ‘h’ occurs before ‘g’ and hence the words are considered to be sorted.
Input: words = {“word”, “world”, “row”}, order = “worldabcefghijkmnpqstuvxyz”
Output: world word row
Explanation: According to the given order ‘l’ occurs before ‘d’ hence the words “world” will be kept first.
方法:给定的问题已经在这里的文章中讨论过。本文提出了一种使用散列的不同方法。由于给出了字母的顺序,它可以用作一个键,其中order[i]个字母可以被第i个字母替换。例如,在给定的 order[] = “hlabcdefgijkmnopqrstuvwxyz” 中,字符'h'可以替换为'a' ,字符'l'可以替换为'b' ,等等。使用该观察,可以通过以下步骤解决给定的问题:
- 创建一个hashFunction ,它接受一个键作为参数,并根据给定的键替换字符串的所有字符,即字符x将替换为存储在key[x]中的字符。
- 使用无序映射encode ,它按照给定的顺序存储字符序列,即encode['h'] = 'a' , encode['l'] = 'b'...等等。类似地,将反转存储在decode中,即decode['a'] = 'h' , decode['b'] = 'l' ... 等等可以用来恢复原始数组。
- 使用encode作为键对数组进行散列后对数组进行排序。
- 使用decode作为键恢复排序数组中的字符串。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
// Function to replace all the
// characters in the array of strings
// according to the given key
void hashFunction(vector& words,
unordered_map key)
{
for (auto& x : words) {
for (int i = 0; i < x.size(); i++) {
x[i] = key[x[i]];
}
}
}
// Function to print the strings
// according to the given order
void printSorted(vector words, string order)
{
// Stores the characters in order
// to encode and decode the string
unordered_map encode, decode;
// Loop to iterate over all characters
for (int i = 0; i < 26; i++) {
// Replace the order[i]th character
// from the ith character
encode[order[i]] = 'a' + i;
// Replace the ith character
// from the order[i]th character
decode['a' + i] = order[i];
}
// Function Call to replace all characters
// in words according to encode
hashFunction(words, encode);
// STL Function to sort the array
sort(words.begin(), words.end());
// Function Call to replace all characters
// in words according to decode in order
// to restore original strings
hashFunction(words, decode);
// Printing the sorted order of words
for (auto x : words) {
cout << x << " ";
}
}
// Driver code
int main()
{
vector words = { "word", "world", "row" };
string order = "worldabcefghijkmnpqstuvxyz";
// Function Call
printSorted(words, order);
return 0;
}
Python3
# Python 3 program of the above approach
# Function to replace all the
# characters in the array of strings
# according to the given key
def hashFunction(words, key):
for x in words:
x = list(x)
for i in range(len(x)):
x[i] = key[x[i]]
x = ''.join(x)
# Function to print the strings
# according to the given order
def printSorted(words, order):
# Stores the characters in order
# to encode and decode the string
encode = {}
decode = {}
# Loop to iterate over all characters
for i in range(26):
# Replace the order[i]th character
# from the ith character
encode[order[i]] = chr(ord('a') + i)
# Replace the ith character
# from the order[i]th character
decode[chr(ord('a') + i)] = order[i]
# Function Call to replace all characters
# in words according to encode
hashFunction(words, encode)
# STL Function to sort the array
words.sort(reverse = True)
# Function Call to replace all characters
# in words according to decode in order
# to restore original strings
hashFunction(words, decode)
# Printing the sorted order of words
for x in words:
print(x,end = " ")
# Driver code
if __name__ == '__main__':
words = ["word", "world", "row"]
order = "worldabcefghijkmnpqstuvxyz"
# Function Call
printSorted(words, order)
# This code is contributed by ipg2016107.
Javascript
world word row
时间复杂度: O(N*M*log(N)) 其中 M 是所有字符串的平均长度。
辅助空间: O(1)