ProblemSolving

Hacker Rank Problem Valid Username Regular Expression Solution

A username is considered valid if all the following constraints are satisfied:

  • The username consists of  8 to 30 characters inclusive, i.e., if the username consists of less than  8 or greater than 30 characters, then it is invalid username.
  • The username must contain alphanumeric characters, and underscores ( ) only. Alphanumeric characters describe the character set consisting of lowercase characters [a-z] uppercase characters [A-Z] , and digits [0-9] .
  • The first character of the username must be an alphabetic character, i.e., either lowercase character [a-z]  or uppercase character [A-Z].

For example:

  •  “Julia” is invalid user name because it contains less than  characters.
  •  “Samantha” is valid as it satisfies all the three constraints for a valid username.
  •  “Samantha_21” is valid as it satisfies all the three constraints for a valid username.
  •  “1Samantha”  is invalid as the first character must be alphabetic.
  •  “Samantha?10_2A” is invalid as it contains the  which does not belong to any of the allowed characters set.

Input Format

The first line of input contains an integer  n, describing the total number of usernames. Each of the next  n lines contains a string describing the username. The locked stub code reads the inputs and validates the username.

Constraints

  • 1 <=n <=100
  • The username consists of any printable characters.

Output Format

For each of the usernames, the locked stub code prints “Valid" if the username is valid; otherwise “Invalid" each on a new line.

Sample Input

8
Julia
Samantha
Samantha_21
1Samantha
Samantha?10_2A
JuliaZ007
Julia@007
_Julia007


Sample Output 0

Invalid
Valid
Valid
Invalid
Invalid
Valid
Invalid
Invalid

Explanation

  • “Julia” is invalid user name because it contains less than  characters.
  •  “Samantha” is valid as it satisfies all the three constraints for a valid username.
  • “Samantha_21” is valid as it satisfies all the three constraints for a valid username.
  • “1Samantha” is invalid as the first character must be alphabetic.
  • “Samantha?10_2A” is invalid as it contains the  which does not belong to any of the allowed characters set.
  • “JuliaZ007” is a valid username as it satisfies all the constraints.
  •  “Julia@007” is an invalid username as it contains the invalid character, .
  • “_Julia007  ” is an invalid username as the first charachter is  which is not an alphabeticcharacter.

Solution.java:

import java.lang.*;
import java.util.Scanner;

class UsernameValidator{
public static final String regularExpression="[a-zA-Z][a-zA-Z0-9_]{7,29}$";
}

public class Solution{
        private static final Scanner scan=new Scanner(System.in);
        public static void main(String[] args){
             int n=Integer.parseInt(scan.nextLine());
             while(n-- !=0 && n<=100 ){
               String userName =scan.nextLine();
               if(userName.matches(UsernameValidator.regularExpression)){
    //No Flags Used
                  System.out.println("Valid");
               } else {
                       System.out.println("Invalid");
                   }
             }
        }
}

  • Few Things To Know Related To RegEx :
  1. a-z : Alphabet range a to z in lowercase
  2. A-Z: Alphabet range a to z in uppercase
  3. 0-9: Number range 0 to 9 digits.
  • The limit the characters in Username we used: {7,29} i.e more than 7 character and less than 29 characters.
  • Expression [a-z]:  Gives a single character in A to Z in lowercase.
  • Similarly [A-Z]: Gives a single character in A to Z in Uppercase.
  • [0-9] : Gives a single digit in range 0 to 9 .

Thank You.

83 thoughts on “Hacker Rank Problem Valid Username Regular Expression Solution”

  1. Howdy very nice web site!! Guy .. Excellent .. Superb .. I’ll bookmark your web site and take the feeds additionallyI am glad to search out so many helpful info here in the put up, we want develop more techniques in this regard, thanks for sharing. ggabedffcceb

  2. Wonderful blog! I found it while surfing around on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Appreciate it afbcdbfekkeafekd

    1. Hello !!! I too Don’t Know that I am on Yahoo News. Key is : Keep Blogging,Keep Your Content Clear and Simple, And posts should be useful to others. Thank you for your time.

  3. Good web site! I really love how it is easy on my eyes and the data are well written. I am wondering how I might be notified whenever a new post has been made. I’ve subscribed to your RSS which must do the trick! Have a great day! fkebddkdfcbk

    1. Thank you for your Interest ,I will get back with you with some solution so ,you and all my friends will be updated with the my new posts.

    1. Thank You. Keep Visiting.If you have any more queries where i can help you,Please have it, so I can try to find solutions for You.

  4. I simply want to say I’m newbie to blogging and site-building and actually enjoyed your website. Likely I’m planning to bookmark your blog . You amazingly have exceptional well written articles. Kudos for revealing your webpage.

  5. I just want to mention I am newbie to weblog and truly enjoyed this web site. Most likely I’m want to bookmark your site . You actually come with really good stories. Regards for sharing with us your web page.

  6. I simply want to say I am all new to blogging and site-building and truly enjoyed your blog. Likely I’m going to bookmark your site . You certainly come with impressive article content. Kudos for sharing with us your blog site.

  7. Oh my goodness! an amazing article dude. Thanks Nonetheless I am experiencing issue with ur rss . Dont know why Unable to subscribe to it. Is there anybody getting similar rss drawback? Anybody who is aware of kindly respond. Thnkx bgedackgakfc

    1. Thanks dude. We are working on subscription ,till then please bookmark the website and keep Visiting.
      Thank You.

  8. I simply want to tell you that I’m newbie to blogging and definitely enjoyed you’re web site. More than likely I’m going to bookmark your blog post . You actually come with amazing article content. Many thanks for sharing with us your blog.

  9. I just want to mention I’m very new to weblog and honestly liked you’re web site. Almost certainly I’m going to bookmark your blog post . You actually have fabulous writings. Cheers for revealing your web site.

  10. I just want to say I am just newbie to blogging and site-building and seriously savored you’re website. Very likely I’m want to bookmark your website . You absolutely have good articles and reviews. Appreciate it for sharing your website page.

  11. I just want to say I’m very new to blogs and definitely enjoyed your web blog. Likely I’m planning to bookmark your blog post . You absolutely come with incredible writings. Thanks for sharing with us your web site.

    1. Wow Great !! I am really Happy to know this from you. A Heartly Thanks to You. Keep Visiting. Keep Sharing. Keep Suggesting me what you would like to read for tommorrow.

    2. Wow Great !! I am really Happy to know this from you. A Heartly Thanks to You. Keep Visiting. Keep Sharing. Keep Suggesting me what you would like to read for tomorrow.

  12. Excellent post however I was wondering if you
    could write a litte more on this subject? I’d be very thankful if you
    could elaborate a little bit further. Appreciate it!

  13. May I just say what a comfort to uncover someone that truly understands
    what they’re talking about online. You certainly realize how to bring a problem to light and make it
    important. More people really need to read this and
    understand this side of your story. I can’t believe you are
    not more popular since you most certainly have the gift.

  14. I like the helpful information you supply on your articles.
    I will bookmark your blog and test once more right here regularly.

    I am fairly sure I will be told a lot of new stuff proper right here!
    Good luck for the following!

  15. Very nice post. I just stumbled upon your blog and wished
    to say that I’ve really enjoyed surfing around your blog posts.
    In any case I’ll be subscribing to your feed and I hope you write again very soon!

    1. Select the content you want to share it by heart and Do Keep Track of What you are trying to say.. Keep it simple, Keep Blogging.. Keep Visiting My Blog. Thanks For Your Time.

    1. Thanks .The Simple thing is you should have habit of sharing things by heart. There we try to give our best. That’s the best fertilizer i would say. Keep Visiting

    1. Thank You for adding my article to your website. I visited your website but i didn’t found my Article. Can You please send me link . Keep Visiting.

  16. “Good blog! I truly love how it is simple on my eyes and the data are well written. I’m wondering how I could be notified when a new post has been made. I have subscribed to your feed which must do the trick! Have a great day!”

    1. Thank For Your Feedback, I will surely get back with some Solution, So I can keep You updated with the new posts. Keep Visiting.

  17. Thank you for some other informative website. Where else may just I am getting that kind of info written in such an ideal means? I’ve a venture that I am just now working on, and I have been on the glance out for such info. eefddbfbecgegebg

  18. I’m still learning from you, but I’m trying to reach my goals. I absolutely enjoy reading all that is posted on your blog.Keep the stories coming. I liked it!

  19. Hey! I just wanted to ask if you ever have any trouble with hackers?
    My last blog (wordpress) was hacked and I ended up
    losing a few months of hard work due to no backup.
    Do you have any methods to stop hackers?

  20. Howdy, i read your blog from time to time and i own a similar one and i was just curious if you get a lot of spam
    responses? If so how do you stop it, any plugin or anything you can recommend?

    I get so much lately it’s driving me crazy so any help is very much appreciated.

    1. For while i don’t pay attention to these spam reply. I am concentrating on quality content which i would like to provide on my blog. Thank You. Keep Reading !! Keep Suggesting !! Keep Commenting !!

  21. I almost never write remarks, but after reading a lot of remarks here. I actually do have a few questions for you if you tend not to mind. Could it be just me or does it look like like a few of these comments appear like they are coming from brain dead individuals? 😛 And, if you are writing at additional social sites, I would like to follow everything fresh you have to post. Would you list of all of your shared pages like your linkedin profile, Facebook page or twitter feed?

  22. An interesting discussion is worth comment. I do think that you need to write much more about this topic, it might become a taboo subject but usually consumers are too little to dicuss on such topics. To the next. Cheers

  23. xy9WPl This is very interesting, You are a very skilled blogger. I have joined your rss feed and look forward to seeking more of your wonderful post. Also, I have shared your web site in my social networks!

Leave a Reply

Your email address will not be published. Required fields are marked *