ANSWER IN C++ ONLY
A string of characters including only alphabets (lowercase letters) is provided as an input.
Let the frequency of a character, lying in between the two characters having same frequency xx, be yy. The steps to be followed for getting the desired arrangement are as follows:
Input:
Output:
Constraints:
Sample Input:
nomatterhowbusyyoumaythinkyouareyoumustfindtimeforreadingorsurrenderyourself toselfchosenignorance 3 3 1 6
Sample Output:
n 8 o 11 m 4 a 5 t 6 e 10 r 10 h 3 w 1 b 1 u 7 s 6 y 6 i 5 k 1 f 4 d 3 g 2 l 2 c 2 k 1 b 1 w 1 n 8 o 11 m 4 a 5 t 6 e 10 r 10 h 3 d 3 g 2 l 2 c 2 u 7 s 6 y 6 i 5 f 4 k 1 b 1 w 1 n 8 o 11 m 4 a 5 t 6 e 10 r 10 h 3 d 3 g 2 l 2 c 2 u 7 s 6 y 6 i 5 f 4 c 2 l 2 g 2 d 3 h 3 k 1 b 1 w 1 n 8 o 11 m 4 a 5 t 6 s 6 y 6 i 5 f 4 e 10 r 10 u 7
EXPLANATION:
First line has characters and their frequency as computed using the given input string. The characters are arranged in the same order as per their appearance in the original string. Second line is obtained after rearranging characters appearing thrice together. Similarly, Third line is generated after placing characters that are coming only once together. The Third line output is similar to the Second line output as targeted characters (having frequency one) automatically comes in continuation during the previous rearrangement corresponding to frequency three. Lastly, the Fourth line presents the final character and frequency sequence after putting characters with frequency 6 together.
PLS HELP
First part
using namespace std;
#define SIZE 26
// function to print the character and its frequency
// in order of its occurrence
void printCharWithFreq(string str)
{
// size of the string 'str'
int n = str.size();
// 'freq[]' implemented as hash table
int freq[SIZE];
// initialize all elements of freq[] to 0
memset(freq, 0, sizeof(freq));
// accumulate freqeuncy of each character in 'str'
for (int i = 0; i < n; i++)
freq[str[i] - 'a']++;
// traverse 'str' from left to right
for (int i = 0; i < n; i++) {
// if frequency of character str[i] is not
// equal to 0
if (freq[str[i] - 'a'] != 0) {
// print the character along with its
// frequency
cout << str[i] << freq[str[i] - 'a'] << " ";
// update frequency of str[i] to 0 so
// that the same character is not printed
// again
freq[str[i] - 'a'] = 0;
}
}
}
// Driver program to test above
int main()
{
string str = "nomatterhowbusyyoumaythinkyouareyoumustfindtimeforreadingorsurrenderyourselftoselfchosenignorance";
printCharWithFreq(str);
return 0;
}
Trying to figure out others too if you like then pls upvote, meanwhile I am working on other subparts of your question
Get Answers For Free
Most questions answered within 1 hours.