![]() |
VOOZH | about |
Given string str, the task is to check whether the given string is a valid GUID (Globally Unique Identifier) or not by using Regular Expression.
The valid GUID (Globally Unique Identifier) must specify the following conditions:
Examples:
Input: str = "123e4567-e89b-12d3-a456-9AC7CBDCEE52"
Output: true
Explanation:
The given string satisfies all the above mentioned conditions. Therefore, it is a valid GUID (Globally Unique Identifier).
Input: str = "123e4567-h89b-12d3-a456-9AC7CBDCEE52"
Output: false
Explanation:
The given string contains 'h', the valid hexadecimal characters should be followed by character from a-f, A-F, and 0-9. Therefore, it is not a valid GUID (Globally Unique Identifier).
Input: str = "123e4567-h89b-12d3-a456"
Output: false
Explanation:
The given string has 20 characters. Therefore, it is not a valid GUID (Globally Unique Identifier).
Approach: The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the answer:
regex = "^[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?$"
Below is the implementation of the above approach:
true true false false
Time Complexity: O(N) for each test case, where N is the length of the given string.
Auxiliary Space: O(1)