Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
largest string formed by choosing words from a given Sentence as per given Pattern
To find the largest string formed by choosing words from a given sentence, a user should know about the lexicographically largest string. A lexicographically largest string is a string, when sorted alphabetically, it would appear at the last if we form all possible strings from the selected words. For example: {lion, zebra, apple} will be lexicographically sorted as {apple, lion, zebra}.
In this article, we have been given a string and a pattern. Based on this pattern, we have to find the lexicographically largest string matching the given pattern by choosing words from the sentence using C. Here is an example for better understanding
Example
Input: string = "slow and steady", pattern = "sdfh" Output: steady
Here is the explanation of the above example
g = (length of pattern / 2) => 4 / 2 = 2 string = "slow and steady", pattern = "sdfh" words = [slow, and, steady] For "slow", matching pattern characters = 1 (s) For "and", matching pattern characters = 0 (no match) For "steady", matching pattern characters = 2 (s, d) Since, steady is the only word that has 2(=g) matching pattern characters, it is selected.
Rules to Form Lexicographically Maximum String
There are 2 rules for lexicographically maximum string that are mentioned below
-
If only one pattern character is matching (i.e. c=1) and it is alphabetically sorted, then it is valid.
Example: string = 'ravi act nice', Pattern = 'ce'. The valid lexicographically maximum string is = 'act' as a<c<t and since 'nice' is not alphabetically sorted it is considered invalid.
-
We calculate g= (length of characters in pattern / 2). If the string contains exactly g pattern characters then it is a valid lexicographically maximum string.
Example: string = 'ravi act nice', Pattern = 'idcf', g = (4/2) = 2. Here valid strings are: 'act'(rule 1) and 'nice'(rule 2).
Algorithm Steps
Here are the steps to form the lexicographically largest string by choosing words from a given sentence as per the given pattern
- Define a string and a pattern sequence.
- The is_sorted() function checks if the string given in the argument is sorted in lexicographical order or not.
- Initialize an array char_count to store the number of occurrences of characters from the pattern string.
- Calculate the value of g as half of the characters in the pattern string.
- Tokenize the sentence into separate words using the strtok_r() function.
- Use a for loop to count the number of characters from the pattern string present.
- Use if/else statement to check the rules for selecting the words from the string.
- Output the valid words that form the largest string matching the pattern.
C Implementation
The following example implements the algorithm to form the lexicographically largest string by choosing words from a given sentence as per the given pattern
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define MAX_STR_LEN 100
// Check if the word is sorted
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;
}
// Function to choose valid strings
void choose_str(char s[], char b[])
{
// To count the frequency of pattern characters
int char_count[256] = {0};
char *token;
char *rest = s;
// Fill the char_count array with pattern characters
for (int i = 0; b[i]; i++)
{
char_count[b[i]]++;
}
int g = strlen(b) / 2; // length of B / 2
int c; // To count pattern characters in current word
char *result[MAX_STR_LEN] = {0};
int result_count = 0;
// To tokenize input sentence and process each word
while ((token = strtok_r(rest, " ", &rest)))
{
c = 0;
int len = strlen(token);
// Count how many pattern characters are in the word
for (int j = 0; j < len; j++)
{
if (char_count[token[j]])
{
c++;
}
}
// Select the word if it satisfies the condition
if ((c == 1 && is_sorted(token)) || c == g)
{
result[result_count] = token;
result_count++;
}
}
printf("Selected words: ");
for (int i = 0; i < result_count; i++)
{
printf("%s ", result[i]);
}
printf("
");
}
int main()
{
char S[MAX_STR_LEN] = "let us learn programming";
char B[MAX_STR_LEN] = "legh";
printf("Input sentence: %s
", S);
printf("Pattern: %s
", B);
printf("g = %lu / 2 = %lu
", strlen(B), strlen(B) / 2);
choose_str(S, B);
return 0;
}
Input sentence: let us learn programming Pattern: legh g = 4 / 2 = 2 Selected words: let learn programming
How It Works
In the above example
- Pattern "legh": g = 4/2 = 2
- Word "let": contains 2 pattern characters (l, e), so it's selected
- Word "us": contains 0 pattern characters, not selected
- Word "learn": contains 2 pattern characters (l, e), so it's selected
- Word "programming": contains 2 pattern characters (g, g), so it's selected
Conclusion
This algorithm efficiently selects words from a sentence based on pattern matching rules. Words are chosen if they contain exactly g pattern characters or if they contain exactly one pattern character and are alphabetically sorted.
