Find Unique ID and Domain Name of a Website from a string
Last Updated :
23 Jul, 2025
Given a string S of size N consisting of unique ID and Domain Name of a unique website, the task is to find the ID and the Domain Name in the given string if the ID is of the form [char, char, char, char, char, digit, digit, digit, digit, char].
Examples:
Input: S = "We thank ABCDE1234F for visiting us and buying products item AMZrr@!k. For more offers, visit us at www.amazon.com"
Output:
ID = ABCDE1234F
Domain = amazon.com
Input: S = "Hi PQRST5678D, it was a pleasure to host you. See www.oyo.com, our official website for future stays"
Output:
ID = PQRST5678D
Domain = oyo.com
Approach: The simplest approach to solve the given problem is to split the string into words and find if the split string is ID or Domain. Follow the steps below to solve the problem:
- First split the words of the string separated by space and store them in a vector of string say words[].
- Initialize two empty strings, say ID and Domain to store the resultant ID and Domain Name.
- Traverse the vector of string words[] and perform the following steps:
- Initialize a variable, say flag as false to store if the current string satisfies the format of ID or not.
- If the length of the current string is 10 and if the first 5 characters and the last character is non-alphabets or any of the remaining characters of the string is non-numeric then mark the flag as true.
- If the value of the flag is false, then assign the current string to ID.
- If the first substring of the current string, say SS over the range [0, 2] is "www" and substring over the range [SS.length() - 3, SS.length() - 1] is "com" then assign the domain name i.e., substring over the range [3, SS.length() - 1] to Domain.
- After completing the above steps, print the value of ID and Domain as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if a character is
// alphabet or not
bool ischar(char x)
{
if ((x >= 'A' && x <= 'Z')
|| (x >= 'a' && x <= 'z')) {
return 1;
}
return 0;
}
// Function to check if a character is
// a numeric or not
bool isnum(char x)
{
if (x >= '0' && x <= '9')
return 1;
return 0;
}
// Function to find ID and Domain
// name from a given string
void findIdandDomain(string S, int N)
{
// Stores ID and the domain names
string ID, Domain;
// Stores the words of string S
vector<string> words;
// Stores the temporary word
string curr = "";
// Traverse the string S
for (int i = 0; i < N; i++) {
// If the current character
// is space
if (S[i] == ' ') {
// Push the curr in words
words.push_back(curr);
// Update the curr
curr = "";
}
// Otherwise
else {
if (S[i] == '.') {
if (i + 1 == N
|| (i + 1 < N
&& S[i + 1] == ' '))
continue;
}
curr += S[i];
}
}
// If curr is not empty
if (curr.length())
words.push_back(curr);
for (string ss : words) {
// If length of ss is 10
if (ss.size() == 10) {
bool flag = 0;
// Traverse the string ss
for (int j = 0; j <= 9; j++) {
// If j is in the range
// [5, 9)
if (j >= 5 && j < 9) {
// If current character
// is not numeric
if (isnum(ss[j]) == 0)
// Mark flag 1
flag = 1;
}
// Otherwise
else {
// If current character
// is not alphabet
if (ischar(ss[j]) == 0)
// Mark flag 1
flag = 1;
}
}
// If flag is false
if (!flag) {
// Assign ss to ID
ID = ss;
}
}
// If substring formed by the
// first 3 character is "www"
// and last 3 character is "moc"
if (ss.substr(0, 3) == "www"
&& ss.substr(
ss.length() - 3, 3)
== "com") {
// Update the domain name
Domain = ss.substr(
4, ss.size() - 4);
}
}
// Print ID and Domain
cout << "ID = " << ID
<< endl;
cout << "Domain = " << Domain;
}
// Driver Code
int main()
{
string S = "We thank ABCDE1234F for visiting "
"us and buying "
"products item AMZrr@!k. For more "
"offers, visit "
"us at www.amazon.com";
int N = S.length();
findIdandDomain(S, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if a character is
// alphabet or not
static boolean ischar(char x)
{
if ((x >= 'A' && x <= 'Z') ||
(x >= 'a' && x <= 'z'))
{
return true;
}
return false;
}
// Function to check if a character is
// a numeric or not
static boolean isnum(char x)
{
if (x >= '0' && x <= '9')
return true;
return false;
}
// Function to find ID and Domain
// name from a given String
static void findIdandDomain(String S, int N)
{
// Stores ID and the domain names
String ID = "", Domain = "";
// Stores the words of String S
Vector<String> words = new Vector<String>();
// Stores the temporary word
String curr = "";
// Traverse the String S
for(int i = 0; i < N; i++)
{
// If the current character
// is space
if (S.charAt(i) == ' ')
{
// Push the curr in words
words.add(curr);
// Update the curr
curr = "";
}
// Otherwise
else
{
if (S.charAt(i) == '.')
{
if (i + 1 == N || (i + 1 < N &&
S.charAt(i + 1) == ' '))
continue;
}
curr += S.charAt(i);
}
}
// If curr is not empty
if (curr.length() > 0)
words.add(curr);
for(String ss : words)
{
// If length of ss is 10
if (ss.length() == 10)
{
boolean flag = false;
// Traverse the String ss
for(int j = 0; j <= 9; j++)
{
// If j is in the range
// [5, 9)
if (j >= 5 && j < 9)
{
// If current character
// is not numeric
if (isnum(ss.charAt(j)) == false)
// Mark flag 1
flag = true;
}
// Otherwise
else
{
// If current character
// is not alphabet
if (ischar(ss.charAt(j)) == false)
// Mark flag 1
flag = true;
}
}
// If flag is false
if (!flag)
{
// Assign ss to ID
ID = ss;
}
}
// If subString formed by the
// first 3 character is "www"
// and last 3 character is "moc"
if (ss.length() > 2 && ss.substring(0, 3).equals("www") &&
ss.substring(ss.length() - 3).equals("com"))
{
// Update the domain name
Domain = ss.substring(4, ss.length());
}
}
// Print ID and Domain
System.out.print("ID = " + ID + "\n");
System.out.print("Domain = " + Domain);
}
// Driver Code
public static void main(String[] args)
{
String S = "We thank ABCDE1234F for visiting " +
"us and buying products item AMZrr@!k. " +
"For more offers, visit us at www.amazon.com";
int N = S.length();
findIdandDomain(S, N);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to check if a character is
# alphabet or not
def ischar(x):
if ((x >= 'A' and x <= 'Z') or
(x >= 'a' and x <= 'z')):
return 1
return 0
# Function to check if a character is
# a numeric or not
def isnum(x):
if (x >= '0' and x <= '9'):
return 1
return 0
# Function to find ID and Domain
# name from a given
def findIdandDomain(S, N):
# Stores ID and the domain names
ID, Domain = "", ""
# Stores the words of S
words = []
# Stores the temporary word
curr = ""
# Traverse the S
for i in range(N):
# If the current character
# is space
if (S[i] == ' '):
# Push the curr in words
words.append(curr)
# Update the curr
curr = ""
# Otherwise
else:
if (S[i] == '.'):
if (i + 1 == N or (i + 1 < N and
S[i + 1] == ' ')):
continue
curr += S[i]
# If curr is not empty
if (len(curr)):
words.append(curr)
for ss in words:
# If length of ss is 10
if (len(ss) == 10):
flag = 0
# Traverse the ss
for j in range(10):
# If j is in the range
# [5, 9)
if (j >= 5 and j < 9):
# If current character
# is not numeric
if (isnum(ss[j]) == 0):
# Mark flag 1
flag = 1
# Otherwise
else:
# If current character
# is not alphabet
if (ischar(ss[j]) == 0):
# Mark flag 1
flag = 1
# If flag is false
if (not flag):
# Assign ss to ID
ID = ss
# If sub formed by the
# first 3 character is "www"
# and last 3 character is "moc"
if (ss[0: 3] == "www" and ss[len(ss) - 3: ]== "com"):
# Update the domain name
Domain = ss[4: len(ss) ]
# Print ID and Domain
print("ID =", ID)
print("Domain =", Domain)
# Driver Code
if __name__ == '__main__':
S = "We thank ABCDE1234F for visiting us "\
"and buying products item AMZrr@!k. "\
"For more offers, visit us at www.amazon.com"
N = len(S)
findIdandDomain(S, N)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to check if a character is
// alphabet or not
static bool ischar(char x)
{
if ((x >= 'A' && x <= 'Z') ||
(x >= 'a' && x <= 'z'))
{
return true;
}
return false;
}
// Function to check if a character is
// a numeric or not
static bool isnum(char x)
{
if (x >= '0' && x <= '9')
return true;
return false;
}
// Function to find ID and Domain
// name from a given String
static void findIdandDoMain(String S, int N)
{
// Stores ID and the domain names
String ID = "", Domain = "";
// Stores the words of String S
List<String> words = new List<String>();
// Stores the temporary word
String curr = "";
// Traverse the String S
for(int i = 0; i < N; i++)
{
// If the current character
// is space
if (S[i] == ' ')
{
// Push the curr in words
words.Add(curr);
// Update the curr
curr = "";
}
// Otherwise
else
{
if (S[i] == '.')
{
if (i + 1 == N || (i + 1 < N &&
S[i + 1] == ' '))
continue;
}
curr += S[i];
}
}
// If curr is not empty
if (curr.Length > 0)
words.Add(curr);
foreach(String ss in words)
{
// If length of ss is 10
if (ss.Length == 10)
{
bool flag = false;
// Traverse the String ss
for(int j = 0; j <= 9; j++)
{
// If j is in the range
// [5, 9)
if (j >= 5 && j < 9)
{
// If current character
// is not numeric
if (isnum(ss[j]) == false)
// Mark flag 1
flag = true;
}
// Otherwise
else
{
// If current character
// is not alphabet
if (ischar(ss[j]) == false)
// Mark flag 1
flag = true;
}
}
// If flag is false
if (!flag)
{
// Assign ss to ID
ID = ss;
}
}
// If subString formed by the
// first 3 character is "www"
// and last 3 character is "moc"
if (ss.Length > 2 && ss.Substring(0, 3).Equals("www") &&
ss.Substring(ss.Length - 3).Equals("com"))
{
// Update the domain name
Domain = ss.Substring(4, ss.Length-4);
}
}
// Print ID and Domain
Console.Write("ID = " + ID + "\n");
Console.Write("Domain = " + Domain);
}
// Driver Code
public static void Main(String[] args)
{
String S = "We thank ABCDE1234F for visiting " +
"us and buying products item AMZrr@!k. " +
"For more offers, visit us at www.amazon.com";
int N = S.Length;
findIdandDoMain(S, N);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JavaScript program for the above approach
// Function to check if a character is
// alphabet or not
function ischar(x)
{
if ((x >= 'A' && x <= 'Z') ||
(x >= 'a' && x <= 'z'))
{
return true;
}
return false;
}
// Function to check if a character is
// a numeric or not
function isnum(x)
{
if (x >= '0' && x <= '9')
return true;
return false;
}
// Function to find ID and Domain
// name from a given string
function findIdandDomain(S, N)
{
// Stores ID and the domain names
let ID, Domain;
// Stores the words of string S
let words = [];
// Stores the temporary word
let curr = "";
// Traverse the string S
for(let i = 0; i < N; i++)
{
// If the current character
// is space
if (S[i] == ' ')
{
// Push the curr in words
words.push(curr);
// Update the curr
curr = "";
}
// Otherwise
else
{
if (S[i] == '.')
{
if (i + 1 == N ||
(i + 1 < N && S[i + 1] == ' '))
continue;
}
curr += S[i];
}
}
// If curr is not empty
if (curr.length >= 1)
words.push(curr);
for(let i = 0; i < words.length; i++)
{
// If length of ss is 10
if (words[i].length == 10)
{
let flag = 0;
// Traverse the string ss
for(let j = 0; j <= 9; j++)
{
// If j is in the range
// [5, 9)
if (j >= 5 && j < 9)
{
// If current character
// is not numeric
if (isnum(words[i][j]) == 0)
// Mark flag 1
flag = 1;
}
// Otherwise
else
{
// If current character
// is not alphabet
if (ischar(words[i][j]) == 0)
// Mark flag 1
flag = 1;
}
}
// If flag is false
if (!flag)
{
// Assign ss to ID
ID = words[i];
}
}
// If substring formed by the
// first 3 character is "www"
// and last 3 character is "moc"
if (words[i].substring(0, 3) == "www" &&
words[i].substring(
words[i].length - 3) == "com")
{
// Update the domain name
Domain = words[i].substring(4);
}
}
// Print ID and Domain
document.write("ID = " + ID + "<br>");
document.write("Domain = " + Domain);
}
// Driver Code
let S = "We thank ABCDE1234F for visiting " +
"us and buying products item AMZrr@!k. " +
"For more offers, visit us at www.amazon.com";
let N = S.length;
findIdandDomain(S, N);
// This code is contributed by Dharanendra L V.
</script>
Output: ID = ABCDE1234F
Domain = amazon.com
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem