- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
largest string formed by choosing words from a given Sentence as per given Pattern
The aim of this article is to implement a program to obtain the Lexicographically largest string formed by choosing words from a given Sentence as per given pattern.
As we know, a string is a group of characters that ends with the null character "\0" in C programming. Characters from the C String are kept in a character array. The main difference between A string and a character array is that a C string differs from a character array in that it ends with the distinctive character "\0."
Example 1
Input: S = “slow and steady”, B = “sdfh” Output: steady
Example 2
Input: S = “an apple a day”, B = “sod” Output: day
Example 3
Input: S = “together we stand”, B = “thfd” Output: together stand
Example 4
Input: S = “let us learn programming”, B = “legh” Output: let learn programming
Problem Statement
Implement a program to obtain the Lexicographically largest string formed by choosing words from given Sentence as per given pattern.
Approach
When only one character is available, determine whether it has been arranged in lexicographically ascending order and, if so, add that character to the output string. When two characters are present in a word of string S, add them to the output string. Organise the output string into lexicographically greatest permutation.
Algorithm
The algorithm to implement a program to obtain the Lexicographically largest string formed by choosing words from given Sentence as per given pattern given below –
Step 1 − Define a string.
Step 2 − Check whether the string is sorted or not sorted.
Step 3 − implement a Function in order to obtain the lexicographically largest string.
Step 4 − Count the frequencies of all the characters in the string.
Step 5 − Now, traverse through the given sentence.
Step 6 − Save the output corresponding to the supplied conditions
Step 7 − Print the output as the result.
Example (C Program)
Here is the C program implementation of the above written algorithm to obtain the Lexicographically largest string formed by choosing words from a given Sentence as per given Pattern.
#include <stdio.h> #include <stdbool.h> #include <string.h> #define MAX_STR_LEN 100 bool is_sorted(char s[]){ int len = strlen(s); for (int i = 0; i < len - 1; i++) { if (s[i] > s[i + 1]) return false; } return true; } void choose_str(char s[], char b[], int n) { int char_count[256] = {0}; char *token; char *rest = s; for (int i = 0; b[i]; i++) { char_count[b[i]]++; } int g = strlen(b) / 2; int c; char *result[MAX_STR_LEN] = {0}; int result_count = 0; while ((token = strtok_r(rest, " ", &rest))) { c = 0; int len = strlen(token); for (int j = 0; j < len; j++){ if (char_count[token[j]]) c++; if (c == g) break; } if ((c == 1 && is_sorted(token)) || c == g) { result[result_count] = token; result_count++; } } for (int i = 0; i < result_count; i++) { printf("%s ", result[i]); } printf("
"); } int main(){ char S[MAX_STR_LEN] = "slow and steady"; char B[MAX_STR_LEN] = "sdfh"; choose_str(S, B, sizeof(S) / sizeof(S[0])); return 0; }
Output
On execution, it will produce the following output −
steady
Conclusion
Likewise, we can implement a program to obtain the Lexicographically largest string formed by choosing words from a given sentence as per given Pattern. The challenge of obtaining the program to find the Lexicographically largest string formed by choosing words from a given Sentence as per given Pattern is resolved in this article.
Here C programming code as well as the algorithm to obtain the Lexicographically largest string formed by choosing words from a given Sentence as per given Pattern are provided.