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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:30:14+00:00 2026-05-28T21:30:14+00:00

I was asked this in an interview, If you to find out if a

  • 0

I was asked this in an interview,
If you to find out if a string consists of only a given set of characters.
For example, let the set of strings be all strings over {0,1,2,3,4,5,6,7,8,9} ie all “numeric” strings. Among this, if the set of strings over {3,8,5} are only valid ones, how do I check if the string consists of only valid characters.
Say:

Input 8888338385
     Output VALID
Input 887837348234 
Output : Invalid

The way that I suggested was brute force, that required checking every character in the given string against a list of invalid characters. If any one of the character was invalid, I’d skip checking all other characters and display the failure message.
However, as suggested here, there may be better algorithms.
Please help.

  • 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-28T21:30:15+00:00Added an answer on May 28, 2026 at 9:30 pm

    EDIT: Thanks to Luc Touraille for a vast improvement to the original algorithm.

    Create an array a[10] of booleans. For each expected digit e, set a[e] = true.

    Now for each digit d in your input, check if a[d] is true. If it’s not, return false. If they all succeed, return true.

    You can generalise this to all ASCII characters with a 256-element array.

    If your input string is length N, your comparison string is length M, and the number of letters in your alphabet is A, then the complexity is O(N+M) (to scan your two strings) plus O(A) (to initialise the boolean array). So unless your string length is close to or greater than your alphabet size, this might not be optimal.

    It’s worth pointing out, with respect to Niklas Baumstark’s excellent performance comparison, that our two solutions are actually the same. The boolean array constructed here is identical to the transition table you’d build in a two-state DFA accepting [c1c2…]*. I’d imagine the only difference is that Java’s implementation, being much more general, carries a lot more overhead.

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

Sidebar

Related Questions

I was asked this question during phone interview. Given two strings find the minimal
This was one of the interview questions asked. How to find the length of
I have recently given an interview. I was asked this question in interview. I
This is a interview Question which was asked and wanted to find an efficient
This question was asked to me in an interview. Given an unsorted list of
I was asked this in my interview today, I applied for a graduate developer
So I was asked this at an interview, but it brought up a good
I was recently asked this question in an interview. Lets suppose I have 2000
Recently I was asked this question in an interview. I gave an answer in
Yesterday in my interview I was asked this question. (At that time I was

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.