
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
- Related Articles
- C program to sort names in alphabetical order
- Find alphabetical order such that words can be considered sorted in C++
- C program to sort names in alphabetical order using structures
- C program to sort names in alphabetical order with string functions.
- Java Program to sort an array in alphabetical order
- Sort the array of strings according to alphabetical order defined by another string in C++
- Python program to sort out words of the sentence in ascending order
- Print common characters of two Strings in alphabetical order in C++
- Sort the words in lexicographical order in C#
- Java program to sort words of sentence in ascending order
- Why are the keys on the keyboard not arranged in alphabetical order?
- C++ program to Reorder the Given String to Form a K-Concatenated String
- Check if the characters of a given string are in alphabetical order in Python
- Python code to print common characters of two Strings in alphabetical order
- Java code to print common characters of two Strings in alphabetical order
