Given string str, the task is to check whether the given string is valid SSN (Social Security Number) or not by using Regular Expression Validate Your SSN Now.
The valid SSN (Social Security Number) must satisfy the following conditions:
ββββIt should have 9 digits.
ββββIt should be divided into 3 parts by hyphen (-).
ββββThe first part should have 3 digits and should not be 000, 666, or between 900 and 999.
ββββThe second part should have 2 digits and it should be from 01 to 99.
ββββThe third part should have 4 digits and it should be from 0001 to 9999.
Examples:
ββββInput: str = β856-45-6789β;
ββββOutput: true
ββββExplanation: The given string satisfies all the above mentioned conditions. Therefore, it is a valid SSN (Social Security Number).
ββββInput: str = β000-45-6789β;
ββββOutput: false
ββββExplanation: The given string starts with 000. Therefore, it is not a valid SSN (Social Security Number).
ββββInput: str = β856-452-6789β;
ββββOutput: false
ββββExplanation: The second part of this string has 3 digits. Therefore, it is not a valid SSN (Social Security Number).
ββββInput: str = β856-45-0000β;
ββββOutput: false
ββββExplanation: The third part of this string is 0000. Therefore, it is not a valid SSN (Social Security Number).
Approach: The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the answer.
Get the String.
Create a regular expression to Check valid SSN (Social Security Number) as mentioned below:
regex = "^(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$";
ββββWhere:
ββββ ^ represents the starting of the string.
ββββ (?!666|000|9\\d{2})\\d{3} represents the first 3 digits should not start with 000, 666, or between 900 and 999.
ββββ β represents the string followed by a hyphen (-).
ββββ (?!00)\\d{2} represents the next 2 digits should not start with 00 and it should be any from 01-99.
ββββ β represents the string followed by a hyphen (-).
ββββ (?!0{4})\\d{4} represents the next 4 digits canβt 0000 and it should be any from 0001-9999.
ββββ $ represents the ending of the string.
ββββMatch the given string with the regular expression. In Java, this can be done by using Pattern.matcher().
ββββReturn true if the string matches with the given regular expression, else return false.
Below is the implementation of the above approach:
C++
// C++ program to validate the
// SSN (Social Security Number)
// using Regular Expression
#include <iostream>
#include <regex>
using namespace std;
// Function to validate the SSN
// (Social Security Number)
bool isValidSSN(string str)
{
ββββ// Regex to check valid SSN
ββββ// (Social Security Number)
ββββconst regex pattern("^(?!666|000|9\\d{2})"
ββββ "\\d{3}-(?!00)"
ββββ "\\d{2}-(?!0{4})\\d{4}$");
β
ββββ// If the SSN (Social Security Number)
ββββ// is empty return false
ββββif (str.empty())
ββββ{
ββββ return false;
ββββ}
β
ββββ// Return true if the SSN
ββββ// (Social Security Number)
ββββ// matched the ReGex
ββββif (regex_match(str, pattern))
ββββ{
ββββ return true;
ββββ}
ββββelse {
ββββ return false;
ββββ}
}
// Driver Code
int main()
{
// Test Case 1:
string str1 = "856-45-6789";
cout << isValidSSN(str1) << endl;
ββββ// Test Case 2:
ββββstring str2 = "000-45-6789";
ββββcout << isValidSSN(str2) << endl;
β
ββββ// Test Case 3:
ββββstring str3 = "856-452-6789";
ββββcout << isValidSSN(str3) << endl;
β
ββββ// Test Case 4:
ββββstring str4 = "856-45-0000";
ββββcout << isValidSSN(str4) << endl;
β
ββββreturn 0;
}
** SSN Validator Java Program
**
// Java program to check valid
// SSN (Social Security Number) using
// regex.
import java.util.regex.*;
class GFG {
ββββ// Function to validate
ββββ// SSN (Social Security Number).
ββββpublic static boolean isValidSSN(String str)
ββββ{
ββββ // Regex to check SSN
ββββ // (Social Security Number).
ββββ String regex = "^(?!666|000|9\\d{2})\\d{3}"
ββββ + "-(?!00)\\d{2}-"
ββββ +"(?!0{4})\\d{4}$";
β
ββββ // Compile the ReGex
ββββ Pattern p = Pattern.compile(regex);
β
ββββ // If the string is empty
ββββ // return false
ββββ if (str == null)
ββββ {
ββββ return false;
ββββ }
β
ββββ // Pattern class contains matcher()
ββββ // method to find matching between
ββββ // given string and regular expression.
ββββ Matcher m = p.matcher(str);
β
ββββ // Return if the string
ββββ // matched the ReGex
ββββ return m.matches();
ββββ}
β
ββββ// Driver Code.
ββββpublic static void main(String args[])
ββββ{
β
ββββ // Test Case 1:
ββββ String str1 = "856-45-6789";
ββββ ;
ββββ System.out.println(isValidSSN(str1));
β
ββββ // Test Case 2:
ββββ String str2 = "000-45-6789";
ββββ ;
ββββ System.out.println(isValidSSN(str2));
β
ββββ // Test Case 3:
ββββ String str3 = "856-452-6789";
ββββ System.out.println(isValidSSN(str3));
β
ββββ // Test Case 4:
ββββ String str4 = "856-45-0000";
ββββ System.out.println(isValidSSN(str4));
ββββ}
}
Python3
import re
def isValidSSN(str):
ββββ# Regex to check valid
ββββ# SSN (Social Security Number).
ββββregex = "^(?!666|000|9\\d{2})\\d{3}-(?!00)\\d{2}-(?!0{4})\\d{4}$"
β
ββββ# Compile the ReGex
ββββp = re.compile(regex)
β
ββββ# If the string is empty
ββββ# return false
ββββif (str == None):
ββββ return False
β
ββββ# Return if the string
ββββ# matched the ReGex
ββββif(re.search(p, str)):
ββββ return True
ββββelse:
ββββ return False
str1 = "856-45-6789"
print(isValidSSN(str1))
str2 = "000-45-6789"
print(isValidSSN(str2))
str3 = "856-452-6789"
print(isValidSSN(str3))
str4 = "856-45-0000"
print(isValidSSN(str4))
C#
// C# program to validate the
// SSN (Social Security Number)
//using Regular Expressions
using System;
using System.Text.RegularExpressions;
class GFG
{
ββββ// Main Method
ββββstatic void Main(string[] args)
ββββ{
ββββ
ββββ // Input strings to Match
ββββ // valid SSN (Social Security Number)
ββββ string[] str={"856-45-6789","000-45-6789" ,"856-452-6789","856-45-0000"};
ββββ foreach(string s in str) {
Console.WriteLine( isValidSSN(s) ? "true" : "false");
}
Console.ReadKey(); }
ββββ// method containing the regex
ββββpublic static bool isValidSSN(string str)
ββββ{
ββββ string strRegex = @"^(?!666|000|9\d{2})\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$";
ββββ Regex re = new Regex(strRegex);
ββββ if (re.IsMatch(str))
ββββ return (true);
ββββ else
ββββ return (false);
ββββ}
}
// Javascript program to validate
// SSN (Social Security Number) using Regular Expression
// Function to validate the
// SSN Code
function isValidSSN(str)
// Regex to check valid
// SSN CODE
let regex = new RegExp(/^(?!666)\d{3}-(?!00)\d{2}-(?!0{4})\d{4}$/);
ββββ// if str CODE
ββββ// is empty return false
ββββif (str == null) {
ββββ return "false";
ββββ}
β
ββββ// Return true if the str
ββββ// matched the ReGex
ββββif (regex.test(str) == true) {
ββββ return "true";
ββββ}
ββββelse {
ββββ return "false";
ββββ}
}
// Driver Code
// Test Case 1:
let str1 = "856-45-6789";
console.log(isValidSSN(str1));
// Test Case 2:
let str2 = "000-45-6789";
console.log(isValidSSN(str2));
// Test Case 3:
let str3 = "856-452-6789";
console.log(isValidSSN(str3));
// Test Case 4:
let str4 = "856-45-0000";
console.log(isValidSSN(str4));
// This code is contributed by Rahul Chauhan
Output
true
false
false
false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
ββββDSA in C++
ββββDSA in Java
ββββDSA in Python
ββββDSA in JavaScript
ββββDSA
ββββPattern Searching