Largest String Formed by Choosing Words from a Given Sentence



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).

Steps to Find Lexicographically Largest String

Here are the steps to form the lexicographically largest string by choosing words from a given sentence as per the given pattern:

  • First, we 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.
  • Then we have initialized an array char_count to store the number of occurrences of characters from the pattern string.
  • Then, the value of g is calculated as half of the characters in the pattern string.
  • Then we tokenized the sentence into separate words using the strtok_r() function.
  • We have used a for loop to count the number of characters from the pattern string present using the char_count array.
  • The if/else statement is then used to check the rules for selecting the words from the string.
  • After selection, the valid words are given as output. The output is the largest string formed by choosing words from a given sentence as per a given pattern string.

C Implementation to Form Lexicographically Largest String from Sentence

The following example implements the steps mentioned above in C 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++;
        }
    }

    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"; choose_str(S, B); return 0; }

The output of the above code is:

let learn programming 
Updated on: 2025-04-16T15:58:58+05:30

246 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements