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 916907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:07:26+00:00 2026-05-15T18:07:26+00:00

Split text into sub-strings according to below rules: a) The length of each sub-string

  • 0

Split text into sub-strings according to below rules:

  • a) The length of each sub-string should less than or equal to M
  • b) The length of sub-string should less than or equal to N (N < M) if the sub-string contains any numeric char
  • c) The total number of sub-strings should be as small as possible

I have no clue how to solve this question, I guess it is related to “dynamic programming”.
Can anybody help me implement it using C# or Java? Thanks a lot.

  • 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-15T18:07:27+00:00Added an answer on May 15, 2026 at 6:07 pm

    Idea

    A greedy approach is the way to go:

    • If the current text is empty, you’re done.
    • Take the first N characters. If any of them is a digit then this is a new substring. Chop it off and go to beginning.
    • Otherwise, extend the digitless segment to at most M characters. This is a new substring. Chop it off and go to beginning.

    Proof

    Here’s a reductio-ad-absurdum proof that the above yields an optimal solution.
    Assume there is a better split than the greedy split. Let’s skip to the point where the two splits start to differ and remove everything before this point.

    Case 1) A digit among the first N characters.

    Assume that there is an input for which chopping off the first N characters cannot yield an optimal solution.

    Greedy split:   |--N--|...
    A better split: |---|--...
                          ^
                          +---- this segment can be shortened from the left side
    

    However, the second segment of the putative better solution can be always shortened from the left side, and the first one extended to N characters, without altering the number of segments. Therefore, a contradiction: this split is not better than the greedy split.

    Case 2) No digit among the first K (N < K <= M) characters.

    Assume that there is an input for which chopping off the first K characters cannot yield an optimal solution.

    Greedy split:   |--K--|...
    A better split: |---|--...
                          ^
                          +---- this segment can be shortened from the left side
    

    Again, the the “better” split can be transformed, without altering the number of segments, to the greedy split, which contradicts the initial assumption that there is a better split than the greedy split.

    Therefore, the greedy split is optimal. Q.E.D.

    Implementation (Python)

    import sys
    
    m, n, text = int(sys.argv[1]), int(sys.argv[2]), sys.argv[3]
    textLen, isDigit = len(text), [c in '0123456789' for c in text]
    
    chunks, i, j = [], 0, 0
    while j < textLen:
       i, j = j, min(textLen, j + n) 
       if not any(isDigit[i:j]):
          while j < textLen and j - i < m and not isDigit[j]:
             j += 1
       chunks += [text[i:j]]
    print chunks
    

    Implementation (Java)

    public class SO {
       public List<String> go(int m, int n, String text) {
          if (text == null)
             return Collections.emptyList();
          List<String> chunks = new ArrayList<String>();
    
          int i = 0;
          int j = 0;
          while (j < text.length()) {
             i = j;         
             j = Math.min(text.length(), j + n);
             boolean ok = true;
             for (int k = i; k < j; k++) 
                if (Character.isDigit(text.charAt(k))) {
                   ok = false;              
                   break;                   
                }                   
             if (ok)        
                while (j < text.length() && j - i < m && !Character.isDigit(text.charAt(j)))
                   j++;                     
             chunks.add(text.substring(i, j));
          }         
          return chunks;
       }    
    
       @Test
       public void testIt() {
          Assert.assertEquals(
             Arrays.asList("asdas", "d332", "4asd", "fsdxf", "23"),
             go(5, 4, "asdasd3324asdfsdxf23"));
       }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 477k
  • Answers 477k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I believe you would need to associate your application with… May 16, 2026 at 5:11 am
  • Editorial Team
    Editorial Team added an answer You can initialize with 0 or nil, and then you… May 16, 2026 at 5:11 am
  • Editorial Team
    Editorial Team added an answer It looks as though you could refactor this into a… May 16, 2026 at 5:11 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.