Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7646793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:17:57+00:00 2026-05-31T10:17:57+00:00

Given an arbitrary string of text, the task is to group the text into

  • 0

Given an arbitrary string of text, the task is to group the text into separate sections of a template. Each section has different min length and max length parameters. A solution can be considered optimal for a section as long as it falls within those bounds. A greedy solution might result in some sections not meeting their minimums, which means the solution as a whole is not acceptable.

I’m having trouble efficiently constructing an algorithm to do this. It seems that a dynamic programming approach might help, but thus far, I haven’t been able to couch it in dynamic programming terms. Does anyone have any leads on solving this problem?

function groupText(str, template)
Inputs:
 str: a string of text
 template: array of JavaScript objects. 
           One object per section that describes the min/max amount of text allowed
Output:
 array: each element corresponds to one section. 
        The value of the element is the text that is in the section.

As an example, let’s define a string str that is equal to “This is a test.” We also have a template t. t consists of several sections. Each section s has a minimum and maximum amount of characters allowed. Let’s say for this example there are only two sections: s1 and s2. s1 has a minimum of 1 character and a maximum of 100. s2 has a minimum of 10 characters and a maximum of 15. We pass our string str and our template t to a function groupText. groupText must return an array, with each element i corresponding to a section. For example, element 0 will correspond to s1. The value of the element will be the text that has been assigned to the section.

In this example, a solution might be.

s1text = “This “

s2text = “is a test.”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:17:59+00:00Added an answer on May 31, 2026 at 10:17 am

    If I understood the problem correctly there’s no need of any search… just subtract from the total length the sum of the minimum lengths and what remains is the amount to be distributed. Then distribute this amount to each element up to its maximum until nothing is left… in code

    var minsum = 0;
    for (vsr i=0; i < sections.length; i++)
        minsum += sections[i].min_size;
    var extra = text.length - minsum;
    if (extra < 0) return null; // no solution
    var solution = [];
    for (var i=0; i < sections.length; i++)
    {
        var x = sections[i].min_size + extra;
        if (x > sections[i].max_size)
            x = sections[i].max_size;
        solution.push(x);
        extra -= x - sections[i].min_size;
    }
    if (extra > 0) return null; // no solution
    return solution;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given any arbitrary, one-line string, my goal is to render it into a bitmap
Given an arbitrary string, what is an efficient method of finding duplicate phrases? We
Question Using XSLT 1.0, given a string with arbitrary characters how can I get
Given two planes positioned and rotated arbitrary to each other, I'm trying to determine
Given an arbitrary string, for example ( I'm going to play croquet next Friday
Given a valid, arbitrary xmlNodePtr, I would like the string representation of that node,
I am writing a c++ app to implement this: Given an arbitrary text document
Given a text file, how would I go about reading an arbitrary line and
Given a string value of arbitrary length, you're supposed to determine the frequency of
Given an arbitrary input string I'm meant to find the sum of all numbers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.