📌  相关文章
📜  根据另一个字符串定义的字母顺序对字符串数组进行排序

📅  最后修改于: 2021-06-25 20:11:52             🧑  作者: Mango

给定一个字符串str和一个字符串strArr []数组,任务是根据str定义的字母顺序对该数组进行排序。
注意: strstrArr []中的每个字符串仅由小写字母组成。
例子:

方法:遍历str的每个字符,并将值存储在以字符及其在数组中的索引为value的映射中。
现在,此地图将作为字符的新字母顺序。开始比较strArr []中的字符串,而不是补偿字符的ASCII值,而是比较映射到映射中那些特定字符的值,即,如果字符c1str中的字符c2之前出现,则c1
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Map to store the characters with their order
// in the new alphabetical order
unordered_map h;
 
// Function that returns true if x < y
// according to the new alphabetical order
bool compare(string x, string y)
{
    for (int i = 0; i < min(x.size(), y.size()); i++) {
        if (h[x[i]] == h[y[i]])
            continue;
        return h[x[i]] < h[y[i]];
    }
    return x.size() < y.size();
}
 
// Driver code
int main()
{
    string str = "fguecbdavwyxzhijklmnopqrst";
    vector v{ "geeksforgeeks", "is", "the",
                      "best", "place", "for", "learning" };
 
    // Store the order for each character
    // in the new alphabetical sequence
    h.clear();
    for (int i = 0; i < str.size(); i++)
        h[str[i]] = i;   
 
    sort(v.begin(), v.end(), compare);
 
    // Print the strings after sorting
    for (auto x : v)
        cout << x << " ";
     
    return 0;
}


Java
// Java implementation of the approach
import java.util.Arrays;
import java.util.Comparator;
 
public class GFG
{
private static void sort(String[] strArr, String str)
{
    Comparator myComp = new Comparator()
    {
        @Override
        public int compare(String a, String b)
        {
            for(int i = 0;
                    i < Math.min(a.length(),
                                 b.length()); i++)
            {
                if (str.indexOf(a.charAt(i)) ==
                    str.indexOf(b.charAt(i)))
                {
                    continue;
                }
                else if(str.indexOf(a.charAt(i)) >
                        str.indexOf(b.charAt(i)))
                {
                    return 1;
                }
                else
                {
                    return -1;
                }
            }
            return 0;
        }
    };
    Arrays.sort(strArr, myComp);
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "fguecbdavwyxzhijklmnopqrst";
    String[] strArr = {"geeksforgeeks", "is", "the", "best",
                       "place", "for", "learning"};
     
    sort(strArr, str);
 
    for(int i = 0; i < strArr.length; i++)
    {
        System.out.print(strArr[i] + " ");
    }
}
}


Python3
# Python3 implementation of the approach
 
# Function to sort and print the array
# according to the new alphabetical order
def sortStringArray(s, a, n):
     
    # Sort the array according to the new alphabetical order
    a = sorted(a, key = lambda word: [s.index(c) for c in word])
    for i in a:
        print(i, end =' ')
 
# Driver code
s = "fguecbdavwyxzhijklmnopqrst"
a = ["geeksforgeeks", "is", "the", "best", "place", "for", "learning"]
n = len(a)
sortStringArray(s, a, n)


输出:
for geeksforgeeks best is learning place the

时间复杂度: O(N * log(N)),其中N是字符串str的大小

辅助空间: O(N)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。