
- 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
Find alphabetical order such that words can be considered sorted in C++
Suppose we have an array of words, we have to find any alphabetical order in the English alphabet so that the given words can be considered sorted in ascending order, if there is any such order exists, otherwise return "impossible".
So, if the input is like words = ["efgh", "wxyz"], then the output will be zyxvutsrqponmlkjihgfewdcba
To solve this, we will follow these steps −
- ALPHABET := 26
- n := size of v
- if n is same as 1, then −
- display "abcdefghijklmnopqrstuvwxyz"
- return
- Define an array adj of size ALPHABET
- Define an array in of size ALPHABET and fill with 0
- pre := v[0]
- for initialize i := 1, when i < n, update (increase i by 1), do −
- s := v[i]
- for j in range 0 to minimum of (size of pre and size of s) - 1 −
- if s[j] is not equal to pre[j], then −
- Come out from the loop
- if j < minimum of size of pre and size of s, then −
- insert s[j] - ASCII of 'a' at the end of adj[pre[j] - ASCII of 'a']
- increase in[s[j] - ASCII of 'a'] by 1
- pre := s
- Ignore following part, skip to the next iteration
- if s[j] is not equal to pre[j], then −
- if size of pre > size of s, then −
- display "Impossible"
- return
- pre := s
- Define one stack my_stack
- for initialize i := 0, when i < ALPHABET, update (increase i by 1), do −
- if in[i] is same as 0, then −
- insert i into my_stack
- Define an array out
- if in[i] is same as 0, then −
- Define an array vis of size: 26. fill with false
- while (my_stack is not empty), do −
- x := top element of my_stack
- delete element from my_stack
- vis[x] := true
- insert x + ASCII of 'a' at the end of out
- for initialize i := 0, when i < size of adj[x], update (increase i by 1), do −
- if vis[adj[x, i]] is non-zero, then −
- Ignore following part, skip to the next iteration
- (decrease in[adj[x, i]] by 1)
- if in[adj[x, i]] is same as 0, then −
- insert adj[x, i] into my_stack
- if vis[adj[x, i]] is non-zero, then −
- for initialize i := 0, when i < ALPHABET, update (increase i by 1), do −
- if not vis[i] is non-zero, then −
- display "Impossible"
- return
- if not vis[i] is non-zero, then −
- for initialize i := 0, when i < size of out, update (increase i by 1), do −
- display out[i]
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; #define ALPHABET 26 void search_ordering(vector<string> v) { int n = v.size(); if (n == 1) { cout << "abcdefghijklmnopqrstuvwxyz"; return; } vector<int> adj[ALPHABET]; vector<int> in(ALPHABET, 0); string pre = v[0]; for (int i = 1; i < n; ++i) { string s = v[i]; int j; for (j = 0; j < min(pre.length(), s.length()); ++j) if (s[j] != pre[j]) break; if (j < min(pre.length(), s.length())) { adj[pre[j] - 'a'].push_back(s[j] - 'a'); in[s[j] - 'a']++; pre = s; continue; } if (pre.length() > s.length()) { cout << "Impossible"; return; } pre = s; } stack<int> my_stack; for (int i = 0; i < ALPHABET; ++i) if (in[i] == 0) my_stack.push(i); vector<char> out; bool vis[26]; memset(vis, false, sizeof(vis)); while (!my_stack.empty()) { char x = my_stack.top(); my_stack.pop(); vis[x] = true; out.push_back(x + 'a'); for (int i = 0; i < adj[x].size(); ++i) { if (vis[adj[x][i]]) continue; in[adj[x][i]]--; if (in[adj[x][i]] == 0) my_stack.push(adj[x][i]); } } for (int i = 0; i < ALPHABET; ++i) if (!vis[i]) { cout << "Impossible"; return; } for (int i = 0; i < out.size(); ++i) cout << out[i]; } int main() { vector<string> v{"efgh", "wxyz"}; search_ordering(v); }
Input
{"efgh", "wxyz"}
Output
zyxvutsrqponmlkjihgfewdcba
- Related Articles
- C++ program to Reorder the Position of the Words in Alphabetical Order
- Maximum number of partitions that can be sorted individually to make sorted in C++
- C program to sort names in alphabetical order
- Get table column names in alphabetical order in MySQL?
- Java Program to sort an array in alphabetical order
- Python – Extract Strings with Successive Alphabets in Alphabetical Order
- C++ program to find how many characters should be rearranged to order string in sorted form
- How a list can be sorted in Java
- Sorting numbers in ascending order and strings in alphabetical order in an array in JavaScript
- C program to sort names in alphabetical order using structures
- Find number of edges that can be broken in a tree such that Bitwise OR of resulting two trees are equal in C++
- Program to find squared elements list in sorted order in Python
- Find the n-th binary string in sorted order in C++
- Count no. of columns that are not sorted in increasing order in C++
- Print common characters of two Strings in alphabetical order in C++

Advertisements