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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:03:28+00:00 2026-05-23T04:03:28+00:00

I’m using a program see here: Visual Basic Regular Expression Question . I enter

  • 0

I’m using a program see here: Visual Basic Regular Expression Question. I enter letters and the program returns all possible combinations from list. I want change this line of code…

Dim result = fruits.Where(Function(fruit) Not fruit.Except(letters).Any())

If I have this list:

 Dim fruit as List(Of string) from {"apple","orange","pear","banana"}

And I input “p a p l e r” then it would return “apple” and “pear”, but if I enter “a p l e r” then it would return just “pear”. The idea is to return all words, which can be made of entered letters without duplicating any single letter. How to optimize this Linq code?

  • 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-23T04:03:29+00:00Added an answer on May 23, 2026 at 4:03 am

    This could be cleaner, but it works:

    Dim fruits As New List(Of String) From { "apple", "orange", "pear", "banana" }
    Dim input As String = "a,p,l,e,r"
    dim inputLetters = from letter in input.Replace(",", "") group by letter into Group select group.first, Group.Count
    
    dim result = fruits.where(
        function(fruit) 
            dim fruitcounts = from letter in fruit group by letter into Group select group.first,  group.count 
            dim res = from fc in fruitcounts, inputs in inputletters where fc.first = inputs.first andalso fc.count <= inputs.count select fc.first
            return res.count = fruit.count
        end function
        )
    

    edit – I removed some unneeded order by clauses, and simplified the grouping

    edit again – after some more thinking, here’s a version that has more lines, but is much clearer and better factored:

    Sub Main
    
        Dim fruits As New List(Of String) From { "apple", "orange", "pear", "banana" }
        Dim input As String = "a,p,l,e,r"
    
        dim matchingFruits = from fruit in fruits where CanBeMadeFrom(fruit, input)
    
    End Sub
    
    Function StringToFrequencyTable(input as string) as Dictionary(of Char, Integer)
    
        dim freqTable = from letter in input 
                        group by letter into Group
                        select letter, Group.Count()
    
        return freqTable.ToDictionary(function(g) g.Letter, function(g) g.Count)
    
    end function
    
    Function CanBeMadeFrom(candidate as string, letters as string) as boolean
    
        dim inputLetters = StringToFrequencyTable(letters.replace(",", ""))
        dim IsCharInFrequencyTable = function(x) (from entry in inputLetters where entry.Key = x.Key andalso entry.Value >= x.Value).Any()
    
        return StringToFrequencyTable(candidate).All( IsCharInFrequencyTable )
    
    end function
    

    If I did anything else to this, I would make CanBeMadeFrom and StringToFrequencyTable into extension methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.