C++ program to Reorder the Position of the Words in Alphabetical Order


In this problem, a string is taken as the input and we have to sort the words present in the string in a lexicographical order. To do that, we assign an index to each word in the string (differentiated with spaces in between) starting from 1, and the output is obtained in the form of sorted index.

String = {“Hello”, “World”}
“Hello” = 1
“World” = 2

Since the words in the input string are already in lexicographical order, the output is printed as “1 2”.

Let us look at some input/result scenarios −

Assuming all the words in the input string are same, let us look at the resultant −

Input: {“hello”, “hello”, “hello”}
Result: 3

The result obtained will be the last position of the word.

Now let us consider the input string with words starting with same letter, the resultant output will be based on the successor letter of the starting letter.

Input: {“Title”, “Tutorial”, “Truth”}
Result: 1 3 2

Another usual input scenario for the method and the result obtained can be seen as below −

Input: {“Welcome”, “To”, “Tutorialspoint”}
Result: 2 3 1

Note − The positions returned are the original positions of these words within the input string. These numbers do not change once the words are sorted within the method.

Algorithm

  • This method is executed using the vector and map abstract data types.

  • The input string is traversed using the auto iterator within the range of the string.

  • The swapping of words in alphabetical order is done by pushing these elements to the back of the vector data type.

  • Once the words are lexicographically rearranged, the original positions of these words in the string are returned as the output.

Example

Let's have an string is [“articles,” “point,” “world”] and the order of the string is −

“articles”: 1
“point”: 2
“world”: 3

We can map each string with the index. We can then sort the strings and then print out the mapped indexes. We can use maps in C++, a sorted data structure to store key-value pairs. Let’s quickly implement our approach.

#include <iostream> #include <vector> #include <map> using namespace std; vector<int> solve(vector<string>& arr) { map<string, int> mp; int index = 1; for(string s : arr) mp[s] = index++; vector<int> res; for(auto it : mp) res.push_back(it.second); return res; } int main() { vector<string> arr = {"articles", "point", "world"}; vector<int> res = solve(arr); for(int i : res) cout << i << " "; return 0; }

Output

1 2 3

Now the reorder of strings will be −

“point,”
“articles,”
“world”

Time Complexity − O(n * log n)

Space Complexity − O(n)

Conclusion

We used a map to do the sorting and mapping both for us. We could have also used a hash map, sorted the vector or array, and then printed the indexes from the hashmap. The time complexity is O(n*log(n)) with O(n) space complexity.

Updated on: 10-Aug-2022

382 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements