📌  相关文章
📜  以字母顺序重新排列单词的位置

📅  最后修改于: 2021-04-29 06:21:24             🧑  作者: Mango

给定一个字符串数组arr [] ,任务是按字典顺序对字符串进行重新排序并在原始列表中打印它们的位置。

例子:

方法:为所有单词分配一个整数,该整数等于它们在数组中的位置。然后按字典顺序对单词列表进行排序,并更改位置,因此从已排序列表中的第一个单词开始打印位置。

下面是上述方法的实现:

C++
// CPP implementation of the approach
#include 
using namespace std;
  
// Function to print the ordering of words
void reArrange(string words[], int n)
{
  
    // Creating list of words and assigning
    // them index numbers
    map mp;
    for (int i = 0; i < n; i++)
        mp[words[i]] = i + 1;
  
    // Sort the list of words
    // lexicographically
    sort(words, words + n);
  
    // Print the ordering
    for (int i = 0; i < n; i++)
        cout << mp[words[i]] << " ";
}
  
// Driver Code
int main()
{
    string words[] = { "live", "place", "travel", "word", "sky" };
    int n = sizeof(words) / sizeof(words[0]);
    reArrange(words, n);
}
  
// This code is contributed by
// Surendra_Gangwar


Java
// Java implementation of the approach
import java.util.*;
class GFG {
  
    // Function to print the ordering of words
    static void reArrange(String words[], int n)
    {
  
        // Creating list of words and assigning
        // them index numbers
        HashMap freq = new HashMap<>();
        for (int i = 0; i < n; i++) {
            freq.put(words[i], (i + 1));
        }
  
        // Sort the list of words
        // lexicographically
        Arrays.sort(words);
  
        // Print the ordering
        for (int i = 0; i < n; i++)
            System.out.print(freq.get(words[i]) + " ");
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        String words[] = { "live", "place", "travel", "word", "sky" };
        int n = words.length;
        reArrange(words, n);
    }
}


Python3
# Python3 implementation of the approach 
  
# Function to print the ordering of words 
def reArrange(words, n):
    # Creating list of words and assigning 
    # them index numbers
    mp = {}
    for i in range(n):
        mp[words[i]] = i + 1
  
    # Sort the list of words
    # lexicographically
    words.sort();
  
    # Print the ordering
    for i in range(n):
        print(mp[words[i]], end = " ") 
  
# Driver Code 
  
words = [ "live", "place", "travel", "word", "sky" ] 
n = len(words) 
reArrange(words, n); 
  
# This code is contributed by 
# Rajnis09


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Function to print the ordering of words
    static void reArrange(String[] words, int n)
    {
  
        // Creating list of words and assigning
        // them index numbers
        Dictionary freq = new Dictionary();
        for (int i = 0; i < n; i++) {
            freq.Add(words[i], (i + 1));
        }
  
        // Sort the list of words
        // lexicographically
        Array.Sort(words);
  
        // Print the ordering
        for (int i = 0; i < n; i++)
            Console.Write(freq[words[i]] + " ");
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        String[] words = { "live", "place", "travel", "word", "sky" };
        int n = words.Length;
        reArrange(words, n);
    }
}
  
// This code contributed by Rajput-Ji


PHP


输出:
1 2 5 3 4