Improve Article
Save Article
Improve Article
Save Article
Given a drawstring S that contains only lowercase alphabets and nary typical characters isolated from spaces, nan task is to find lexicographically adjacent pairs of characters and concatenate nan number of pairs of each connection successful nan string.
Examples:
Input: S = “hello world”
Output: 21
Explanation: Input drawstring contains 2 words “hello” and “world”, In connection ‘hello’, ‘e’ & ‘l’ and ‘l’ & ‘o’ are successful lexicographical bid truthful nan brace count for “hello” is 2. In connection “world” only ‘o’ & ‘r’ are successful lexical bid truthful brace count for “world” is 1. On concatenating some consequence we get 21 arsenic output.
Input: S = “venom vest”
Output: 22
Explanation: Input drawstring contains 2 words “venom” and “vest”, In connection “venom”, ‘e’ & ‘n’ and ‘n’ & ‘o’ are successful lexicographical bid truthful nan brace count for “venom” is 2. In connection “vest”, ‘e’ & ‘s’ and ‘s’ & ‘t’ are successful lexical bid truthful brace count for “vest” is 2. On concatenating some consequence we get 22 arsenic output.
Input: S = “abcdefghijklmn mno”
Output: 132
Explanation: Input drawstring contains 2 words, In first connection “abcdefghijklmn”, each azygous adjacent characteristic follows nan lexical bid frankincense location are full 13 pairs and for 2nd connection “mno” location are 2 pairs.On concatenating some consequence we get 132 arsenic output.
Approach: To lick nan problem travel nan beneath idea:
The attack is to initially find nan number of pairs successful each connection and past usage that brace count for concatenating truthful this tin beryllium done successful nan beneath steps:
Steps to travel to lick nan problem:
- Initialize a vector of drawstring to extract words from nan input drawstring and past initialize nan int vector of size n ( nan size of nan drawstring vector ) to support nan count of pairs successful each word.
- Use istringstream to extract words from nan input drawstring and shop each connection into a drawstring vector.
- Iterate complete drawstring vector and for each azygous iteration, execute nan cognition mentioned below:
- Take nan existent connection and shop it successful a temp drawstring and iterate complete nan temp drawstring and cheque whether nan ASCII values of 2 adjacent characters travel nan lexical bid aliases not. If Yes, past return nan brace count worth and increment it.
- Store nan brace count successful an integer vector.
- After this initialize an integer adaptable for nan last reply pinch nan first worth of nan integer vector and past iterate complete nan vector values for concatenating.
Below is nan implementation for nan supra approach:
C++
Time Complexity: O(3N) //First N for iterating complete input drawstring for extracting words + Second N for processing each connection to find brace + Third N for generating unsocial code.
Auxiliary Space: O(2N)//First N for storing words successful drawstring vector + Second N for storing brace counts successful an integer vector