The Wayback Machine - https://web.archive.org/web/20240910181130/https://www.geeksforgeeks.org/implement-reverse-dns-look-cache/
Open In App

How to Implement Reverse DNS Look Up Cache?

Last Updated : 25 Apr, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Reverse DNS look up is using an internet IP address to find a domain name. For example, if you type 74.125.200.106 in browser, it automatically redirects to google.in. 
How to implement Reverse DNS Look Up cache? Following are the operations needed from cache:

  • Add an IP address to URL Mapping in cache.
  • Find URL for a given IP address.

One solution is to use Hashing. 
In this post, a Trie based solution is discussed. One advantage of Trie based solutions is, the worst case upper bound is O(1) for Trie, for hashing, the best possible average case time complexity is O(1). Also, with Trie we can implement prefix search (finding all urls for a common prefix of IP addresses). 
The general disadvantage of Trie is large amount of memory requirement, this is not a major problem here as the alphabet size is only 11 here. Ten characters are needed for digits from ‘0’ to ‘9’ and one for dot (‘.’). 
The idea is to store IP addresses in Trie nodes and in the last node we store the corresponding domain name. Following is C style implementation in C++.
 

CPP




// C++ based program to implement reverse DNS lookup
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
 
// There are atmost 11 different chars in a valid IP address
#define CHARS 11
 
// Maximum length of a valid IP address
#define MAX 50
 
// A utility function to find index of child for a given character 'c'
int getIndex(char c) { return (c == '.')? 10: (c - '0'); }
 
// A utility function to find character for a given child index.
char getCharFromIndex(int i) { return (i== 10)? '.' : ('0' + i); }
 
// Trie Node.
struct trieNode
{
    bool isLeaf;
    char *URL;
    struct trieNode *child[CHARS];
};
 
// Function to create a new trie node.
struct trieNode *newTrieNode(void)
{
    struct trieNode *newNode = new trieNode;
    newNode->isLeaf = false;
    newNode->URL = NULL;
    for (int i=0; i<CHARS; i++)
        newNode->child[i] = NULL;
    return newNode;
}
 
// This method inserts an ip address and the corresponding
// domain name in the trie. The last node in Trie contains the URL.
void insert(struct trieNode *root, char *ipAdd, char *URL)
{
    // Length of the ip address
    int len = strlen(ipAdd);
    struct trieNode *pCrawl = root;
 
    // Traversing over the length of the ip address.
    for (int level=0; level<len; level++)
    {
        // Get index of child node from current character
        // in ipAdd[].  Index must be from 0 to 10 where
        // 0 to 9 is used for digits and 10 for dot
        int index = getIndex(ipAdd[level]);
 
        // Create a new child if not exist already
        if (!pCrawl->child[index])
            pCrawl->child[index] = newTrieNode();
 
        // Move to the child
        pCrawl = pCrawl->child[index];
    }
 
    //Below needs to be carried out for the last node.
    //Save the corresponding URL of the ip address in the
    //last node of trie.
    pCrawl->isLeaf = true;
    pCrawl->URL = new char[strlen(URL) + 1];
    strcpy(pCrawl->URL, URL);
}
 
// This function returns URL if given IP address is present in DNS cache.
// Else returns NULL
char  *searchDNSCache(struct trieNode *root, char *ipAdd)
{
    // Root node of trie.
    struct trieNode *pCrawl = root;
    int  len = strlen(ipAdd);
 
    // Traversal over the length of ip address.
    for (int level=0; level<len; level++)
    {
        int index = getIndex(ipAdd[level]);
        if (!pCrawl->child[index])
            return NULL;
        pCrawl = pCrawl->child[index];
    }
 
    // If we find the last node for a given ip address, print the URL.
    if (pCrawl!=NULL && pCrawl->isLeaf)
        return pCrawl->URL;
 
    return NULL;
}
 
//Driver function.
int main()
{
    /* Change third ipAddress for validation */
    char ipAdd[][MAX] = {"107.108.11.123", "107.109.123.255",
                         "74.125.200.106"};
    char URL[][50] = {"www.samsung.com", "www.samsung.net",
                      "www.google.in"};
    int n = sizeof(ipAdd)/sizeof(ipAdd[0]);
    struct trieNode *root = newTrieNode();
 
    // Inserts all the ip address and their corresponding
    // domain name after ip address validation.
    for (int i=0; i<n; i++)
        insert(root,ipAdd[i],URL[i]);
 
    // If reverse DNS look up succeeds print the domain
    // name along with DNS resolved.
    char ip[] = "107.108.11.123";
    char *res_url = searchDNSCache(root, ip);
    if (res_url != NULL)
        printf("Reverse DNS look up resolved in cache:\n%s --> %s",
                ip, res_url);
    else
        printf("Reverse DNS look up not resolved in cache ");
    return 0;
}


Java




// Java based program to implement reverse DNS lookup
import java.util.HashMap;
 
// Maximum length of a valid IP address
class TrieNode {
    HashMap<Character, TrieNode> children;
    boolean isLeaf;
    String URL;
  
    public TrieNode() {
        children = new HashMap<>();
        isLeaf = false;
        URL = null;
    }
}
  
// Class to represent DNS Cache
class DNSCache {
    // Root of Trie
    TrieNode root;
  
    // Constructor
    public DNSCache() {
        root = new TrieNode();
    }
  
    // Function to insert a new domain value with its corresponding
    // IP address in the DNS cache
    public void insert(String ipAdd, String URL) {
        // Length of the ip address
        int len = ipAdd.length();
  
        // Traversing over the length of the ip address.
        TrieNode pCrawl = root;
        for (int level=0; level<len; level++) {
  
            // Get index of child node from current character
            // in ipAdd[].  Index must be from 0 to 10 where
            // 0 to 9 is used for digits and 10 for dot
            char c = ipAdd.charAt(level);
            int index = getIndex(c);
  
            // Create a new child if not exist already
            if (pCrawl.children.get(c) == null)
                pCrawl.children.put(c, new TrieNode());
  
            // Move to the child
            pCrawl = pCrawl.children.get(c);
        }
  
        //Below needs to be carried out for the last node.
        //Save the corresponding URL of the ip address in the
        //last node of trie.
        pCrawl.isLeaf = true;
        pCrawl.URL = URL;
    }
  
    // This function returns URL if given IP address is present in DNS cache.
    // Else returns null
    public String searchDNSCache(String ipAdd) {
        // Root node of trie.
        TrieNode pCrawl = root;
        int  len = ipAdd.length();
  
        // Traversal over the length of ip address.
        for (int level=0; level<len; level++) {
            char c = ipAdd.charAt(level);
            int index = getIndex(c);
            if (pCrawl.children.get(c) == null)
                return null;
            pCrawl = pCrawl.children.get(c);
        }
  
        // If we find the last node for a given ip address, print the URL.
        if (pCrawl!=null && pCrawl.isLeaf)
            return pCrawl.URL;
  
        return null;
    }
  
    // A utility function to find index of child for a given character 'c'
    public int getIndex(char c) { return (c == '.')? 10: (c - '0'); }
  
    // Driver function
    public static void main(String args[]) {
        /* Change third ipAddress for validation */
        String ipAdd[] = {"107.108.11.123", "107.109.123.255",
                          "74.125.200.106"};
        String URL[] = {"www.samsung.com", "www.samsung.net",
                        "www.google.in"};
        int n = ipAdd.length;
        DNSCache dns = new DNSCache();
  
        // Inserts all the ip address and their corresponding
        // domain name after ip address validation.
        for (int i = 0; i < n; i++)
            dns.insert(ipAdd[i],URL[i]);
  
        // If reverse DNS look up succeeds print the domain
        // name along with DNS resolved.
        String ip = "107.108.11.123";
        String res_url = dns.searchDNSCache(ip);
        if (res_url != null)
            System.out.println("Reverse DNS look up resolved in cache:\n" + ip + " --> " + res_url);
        else
            System.out.println("Reverse DNS look up not resolved in cache ");
    }
}


Python3




# Trie Node.
class TrieNode:
    def __init__(self):
        self.isLeaf = False
        self.URL = None
        self.child = [None]*11
 
# Function to create a new trie node.
def newTrieNode():
    return TrieNode()
 
# A utility function to find index of child for a given character 'c'
def getIndex(c):
    if c == '.':
        return 10
    else:
        return ord(c) - ord('0')
 
# A utility function to find character for a given child index.
def getCharFromIndex(i):
    if i == 10:
        return '.'
    else:
        return chr(i + ord('0'))
 
# This method inserts an ip address and the corresponding
# domain name in the trie. The last node in Trie contains the URL.
def insert(root, ipAdd, URL):
    # Length of the ip address
    len_ipAdd = len(ipAdd)
    pCrawl = root
 
    # Traversing over the length of the ip address.
    for level in range(len_ipAdd):
        # Get index of child node from current character
        # in ipAdd[].  Index must be from 0 to 10 where
        # 0 to 9 is used for digits and 10 for dot
        index = getIndex(ipAdd[level])
 
        # Create a new child if not exist already
        if not pCrawl.child[index]:
            pCrawl.child[index] = newTrieNode()
 
        # Move to the child
        pCrawl = pCrawl.child[index]
 
    #Below needs to be carried out for the last node.
    #Save the corresponding URL of the ip address in the
    #last node of trie.
    pCrawl.isLeaf = True
    pCrawl.URL = URL
 
# This function returns URL if given IP address is present in DNS cache.
# Else returns None
def searchDNSCache(root, ipAdd):
    # Root node of trie.
    pCrawl = root
    len_ipAdd = len(ipAdd)
 
    # Traversal over the length of ip address.
    for level in range(len_ipAdd):
        index = getIndex(ipAdd[level])
        if not pCrawl.child[index]:
            return None
        pCrawl = pCrawl.child[index]
 
    # If we find the last node for a given ip address, print the URL.
    if pCrawl and pCrawl.isLeaf:
        return pCrawl.URL
 
    return None
 
#Driver function.
if __name__ == "__main__":
    # Change third ipAddress for validation
    ipAdd = ["107.108.11.123", "107.109.123.255", "74.125.200.106"]
    URL = ["www.samsung.com", "www.samsung.net", "www.google.in"]
    n = len(ipAdd)
    root = newTrieNode()
 
    # Inserts all the ip address and their corresponding
    # domain name after ip address validation.
    for i in range(n):
        insert(root, ipAdd[i], URL[i])
 
 
    # If reverse DNS look up succeeds print the domain
    # name along with the IP address.
    ip = "107.108.11.123"
    name = searchDNSCache(root, ip)
    if name != None:
        print("Reverse DNS look up resolved in cache:\n" + ip + " --> " + name)
    else:
        print("Reverse DNS look up failed for " + ip)


C#




// C# based program to implement reverse DNS lookup
using System;
 
public class GFG {
 
  // There are atmost 11 different chars in a valid IP
  // address
  const int CHARS = 11;
 
  // Maximum length of a valid IP address
  const int MAX = 50;
 
  // A utility function to find index of child for a given
  // character 'c'
  static int GetIndex(char c)
  {
    return (c == '.') ? 10 : (c - '0');
  }
 
  // A utility function to find character for a given
  // child index.
  static char GetCharFromIndex(int i)
  {
    return (i == 10) ? '.' : (char)(i + '0');
  }
 
  // Trie Node.
  class TrieNode {
    public bool IsLeaf
    {
      get;
      set;
    }
    public string URL
    {
      get;
      set;
    }
    public TrieNode[] Child
    {
      get;
      set;
    }
 
    // Function to create a new trie node.
    public TrieNode()
    {
      IsLeaf = false;
      URL = null;
      Child = new TrieNode[CHARS];
      for (int i = 0; i < CHARS; i++) {
        Child[i] = null;
      }
    }
  }
 
  // This method inserts an ip address and the
  // corresponding domain name in the trie. The last node
  // in Trie contains the URL.
  static void Insert(TrieNode root, string ipAdd,
                     string URL)
  {
 
    // Length of the ip address
    int len = ipAdd.Length;
    TrieNode pCrawl = root;
 
    // Traversing over the length of the ip address.
    for (int level = 0; level < len; level++) {
 
      // Get index of child node from current
      // character in ipAdd[].  Index must be from 0
      // to 10 where 0 to 9 is used for digits and 10
      // for dot
      int index = GetIndex(ipAdd[level]);
 
      // Create a new child if not exist already
      if (pCrawl.Child[index] == null) {
        pCrawl.Child[index] = new TrieNode();
      }
 
      // Move to the child
      pCrawl = pCrawl.Child[index];
    }
 
    // Below needs to be carried out for the last node.
    // Save the corresponding URL of the ip address in
    // the last node of trie.
    pCrawl.IsLeaf = true;
    pCrawl.URL = URL;
  }
 
  // This function returns URL if given IP address is
  // present in DNS cache. Else returns NULL
  static string SearchDNSCache(TrieNode root,
                               string ipAdd)
  {
 
    // Root node of trie.
    TrieNode pCrawl = root;
    int len = ipAdd.Length;
 
    // Traversal over the length of ip address.
    for (int level = 0; level < len; level++) {
      int index = GetIndex(ipAdd[level]);
      if (pCrawl.Child[index] == null) {
        return null;
      }
      pCrawl = pCrawl.Child[index];
    }
 
    // If we find the last node for a given ip address,
    // print the URL.
    if (pCrawl != null && pCrawl.IsLeaf) {
      return pCrawl.URL;
    }
 
    return null;
  }
 
  // Driver function.
  static public void Main(string[] args)
  {
 
    /* Change third ipAddress for validation */
    string[] ipAdd
      = { "107.108.11.123", "107.109.123.255",
         "74.125.200.106" };
    string[] URL
      = { "www.samsung.com", "www.samsung.net",
         "www.google.in" };
    int n = ipAdd.Length;
    TrieNode root = new TrieNode();
 
    // Inserts all the ip address and their
    // corresponding domain name after ip address
    // validation.
    for (int i = 0; i < n; i++) {
      Insert(root, ipAdd[i], URL[i]);
    }
 
    // If reverse DNS look up succeeds print the domain
    // name along with DNS resolved.
    string ip = "107.108.11.123";
    string res_url = SearchDNSCache(root, ip);
    if (res_url != null) {
      Console.WriteLine(
        "Reverse DNS look up resolved in cache:");
      Console.WriteLine(ip + " --> " + res_url);
    }
    else {
      Console.WriteLine(
        "Reverse DNS look up not resolved in cache ");
    }
  }
}
 
// This code is contributed by prasad264


Javascript




// Trie Node.
class TrieNode {
    constructor() {
        this.isLeaf = false;
        this.URL = null;
        this.child = Array(11).fill(null);
    }
}
 
// Function to create a new trie node.
function newTrieNode() {
    return new TrieNode();
}
 
// A utility function to find index of child for a given character 'c'
function getIndex(c) {
    if (c === '.') {
        return 10;
    } else {
        return c.charCodeAt(0) - '0'.charCodeAt(0);
    }
}
 
// A utility function to find character for a given child index.
function getCharFromIndex(i) {
    if (i === 10) {
        return '.';
    } else {
        return String.fromCharCode(i + '0'.charCodeAt(0));
    }
}
 
// This method inserts an ip address and the corresponding
// domain name in the trie. The last node in Trie contains the URL.
function insert(root, ipAdd, URL) {
    // Length of the ip address
    let len_ipAdd = ipAdd.length;
    let pCrawl = root;
 
    // Traversing over the length of the ip address.
    for (let level = 0; level < len_ipAdd; level++) {
        // Get index of child node from current character
        // in ipAdd[].  Index must be from 0 to 10 where
        // 0 to 9 is used for digits and 10 for dot
        let index = getIndex(ipAdd[level]);
 
        // Create a new child if not exist already
        if (!pCrawl.child[index]) {
            pCrawl.child[index] = newTrieNode();
        }
 
        // Move to the child
        pCrawl = pCrawl.child[index];
    }
 
    // Below needs to be carried out for the last node.
    // Save the corresponding URL of the ip address in the
    // last node of trie.
    pCrawl.isLeaf = true;
    pCrawl.URL = URL;
}
 
// This function returns URL if given IP address is present in DNS cache.
// Else returns None
function searchDNSCache(root, ipAdd) {
    // Root node of trie.
    let pCrawl = root;
    let len_ipAdd = ipAdd.length;
 
    // Traversal over the length of ip address.
    for (let level = 0; level < len_ipAdd; level++) {
        let index = getIndex(ipAdd[level]);
        if (!pCrawl.child[index]) {
            return null;
        }
        pCrawl = pCrawl.child[index];
    }
 
    // If we find the last node for a given ip address, print the URL.
    if (pCrawl && pCrawl.isLeaf) {
        return pCrawl.URL;
    }
    return null;
}
 
//Driver function.
let ipAdd = ["107.108.11.123", "107.109.123.255", "74.125.200.106"];
let URL = ["www.samsung.com", "www.samsung.net", "www.google.in"];
let n = ipAdd.length;
let root = newTrieNode();
 
// Inserts all the ip address and their corresponding
// domain name after ip address validation.
for (let i = 0; i < n; i++) {
    insert(root, ipAdd[i], URL[i]);
}
 
// If reverse DNS look up succeeds print the domain
// name along with the IP address.
let ip = "107.108.11.123";
let name = searchDNSCache(root, ip);
if (name != null) {
    console.log("Reverse DNS look up resolved in cache:\n" + ip + " --> " + name);
} else {
    console.log("Reverse DNS look up failed for " + ip);
}


Output: 

Reverse DNS look up resolved in cache:
107.108.11.123 --> www.samsung.com

 

Time complexity- The time complexity of the insert function is O(k), where k is the length of the IP address being inserted. The searchDNSCache function also has a time complexity of O(k), as it traverses the trie for the given IP address.

Space complexity – The space complexity of the program is O(nm), where n is the number of IP addresses and m is the length of the longest URL. This is because we are storing the URL for each IP address in the trie, which can take up to m space. Additionally, we are creating a new trie node for each level of the IP address, which can take up to 11 pointers and a boolean flag, or 12 units of space per node. Therefore, the total space complexity is O(nm12), or O(nm).

Note that the above implementation of Trie assumes that the given IP address does not contain characters other than {‘0’, ‘1’,….. ‘9’, ‘.’}. What if a user gives an invalid IP address that contains some other characters? This problem can be resolved by validating the input IP address before inserting it into Trie. We can use the approach discussed here for IP address validation.
Java implementation is given below: 
 

C++




#include <iostream>
#include <unordered_map>
#include <vector>
 
using namespace std;
 
class TrieNode {
public:
    unordered_map<char, TrieNode*> child;
    string url;
 
    TrieNode() { this->url = ""; }
 
    ~TrieNode()
    {
        for (auto it = child.begin(); it != child.end();
             ++it) {
            delete it->second;
        }
    }
};
 
class ReverseDNSLookup {
public:
    void insert(TrieNode* node, vector<string> ipAdd,
                vector<string> urls)
    {
        for (int i = 0; i < ipAdd.size(); ++i) {
            insertUtil(node, ipAdd[i], urls[i], 0);
        }
    }
 
    void insertUtil(TrieNode* node, string ipAddr,
                    string url, int pos)
    {
        TrieNode* temp = nullptr;
        if (node->child.find(ipAddr[pos])
            != node->child.end()) {
            temp = node->child[ipAddr[pos]];
        }
        else {
            temp = new TrieNode();
            node->child[ipAddr[pos]] = temp;
        }
        if (pos == ipAddr.size() - 1) {
            temp->url = url;
            return;
        }
        insertUtil(temp, ipAddr, url, pos + 1);
    }
 
    string search(TrieNode* node, string ipAddr, int pos)
    {
        TrieNode* temp = nullptr;
        if (pos == ipAddr.size() - 1) {
            if (node->child.find(ipAddr[pos])
                != node->child.end()) {
                temp = node->child[ipAddr[pos]];
                if (!temp->url.empty()) {
                    return temp->url;
                }
            }
        }
        if (node->child.find(ipAddr[pos])
            != node->child.end()) {
            temp = node->child[ipAddr[pos]];
            return search(temp, ipAddr, pos + 1);
        }
        return "No url associated/Invalid IP address";
    }
};
 
int main()
{
    ReverseDNSLookup r;
    vector<string> ipAdd
        = { "107.108.11.123", "107.109.123.255",
            "74.125.200.106" };
    vector<string> urls
        = { "www.samsung.com", "www.samsung.net",
            "www.google.in" };
 
    TrieNode* root = new TrieNode();
    r.insert(root, ipAdd, urls);
    cout << "74.125.200.106 : "
         << r.search(root, "74.125.200.106", 0) << endl;
    cout << "107.109.123.245 : "
         << r.search(root, "107.109.123.245", 0) << endl;
 
    delete root;
 
    return 0;
}
// This code is contributed by prasad264


Java




import java.util.HashMap;
import java.util.Map;
 
public class ReverseDNSLookup
{
    public void insert(TrieNode node, String[] ipAdd, String[] urls)
    {
        for(int i=0;i<ipAdd.length;i++)
            this.insertUtil(node, ipAdd[i], urls[i], 0);
    }
 
    public void insertUtil(TrieNode node, String ipAddr, String url, int pos)
    {
        TrieNode temp = null;
        if(node.child.containsKey(ipAddr.charAt(pos)))
        {
            temp = node.child.get(ipAddr.charAt(pos));
        }
        else
        {
            temp = new TrieNode();
            node.child.put(ipAddr.charAt(pos), temp);
        }
        if(pos==ipAddr.length()-1)
        {
            temp.url = url;
            return;
        }
        this.insertUtil(temp, ipAddr, url, pos+1);
    }
 
    public String search(TrieNode node, String ipAddr, int pos)
    {
        TrieNode temp = null;
        if(pos==ipAddr.length()-1)   
        {
            temp = node.child.get(ipAddr.charAt(pos));
            if(temp!=null)
                return temp.url;
        }
        if(node.child.containsKey(ipAddr.charAt(pos)))
        {   
            temp = node.child.get(ipAddr.charAt(pos));
            return this.search(temp, ipAddr, pos+1);       
        }
        return "No url associated/Invalid IP address";
    }
 
    public static void main(String []args)
    {
        ReverseDNSLookup r = new ReverseDNSLookup();
        String[] ipAdd = {"107.108.11.123",
                                  "107.109.123.255", "74.125.200.106"};
        String[] urls = {"www.samsung.com",
                         "www.samsung.net", "www.google.in"};
 
        TrieNode root = new TrieNode();
        r.insert(root, ipAdd, urls);
        //System.out.println(root);
        System.out.println("74.125.200.106 : "+
                                r.search(root, "74.125.200.106", 0));
        System.out.println("107.109.123.245 : "+
                                r.search(root, "107.109.123.245", 0));
    }
}
 
class TrieNode
{
    Map<Character, TrieNode> child;
    String url;
 
    TrieNode()
    {
        this.child = new HashMap<>();
    }
 
    public String toString()
    {
        return child.toString()+" : "+url;
    }
}
 
// This code is contributed by Akhilesh Singla


Python3




class TrieNode:
    def __init__(self):
        self.child = {}
        self.url = ""
 
    def __str__(self):
        return str(self.child) + " : " + self.url
 
 
class ReverseDNSLookup:
    def insert(self, node, ipAdd, urls):
        for i in range(len(ipAdd)):
            self.insertUtil(node, ipAdd[i], urls[i], 0)
 
    def insertUtil(self, node, ipAddr, url, pos):
        temp = None
        if ipAddr[pos] in node.child:
            temp = node.child[ipAddr[pos]]
        else:
            temp = TrieNode()
            node.child[ipAddr[pos]] = temp
        if pos == len(ipAddr) - 1:
            temp.url = url
            return
        self.insertUtil(temp, ipAddr, url, pos + 1)
 
    def search(self, node, ipAddr, pos):
        temp = None
        if pos == len(ipAddr) - 1:
            temp = node.child.get(ipAddr[pos])
            if temp:
                return temp.url
        if ipAddr[pos] in node.child:
            temp = node.child[ipAddr[pos]]
            return self.search(temp, ipAddr, pos + 1)
        return "No url associated/Invalid IP address"
 
 
def main():
    r = ReverseDNSLookup()
    ipAdd = ["107.108.11.123", "107.109.123.255", "74.125.200.106"]
    urls = ["www.samsung.com", "www.samsung.net", "www.google.in"]
 
    root = TrieNode()
    r.insert(root, ipAdd, urls)
    print("74.125.200.106 : " + r.search(root, "74.125.200.106", 0))
    print("107.109.123.245 : " + r.search(root, "107.109.123.245", 0))
 
if __name__ == "__main__":
    main()


Javascript




<script>
 
class TrieNode
{
    constructor()
    {
        this.child = new Map();
    }
    toString()
    {
        return this.child.toString()+" : "+url;
    }
}
 
function insert(node,ipAdd,urls)
{
    for(let i = 0; i < ipAdd.length; i++)
            this.insertUtil(node, ipAdd[i], urls[i], 0);
}
 
function insertUtil(node, ipAddr,url,pos)
{
    let temp = null;
        if(node.child.has(ipAddr[pos]))
        {
            temp = node.child.get(ipAddr[pos]);
        }
        else
        {
            temp = new TrieNode();
            node.child.set(ipAddr[pos], temp);
        }
        if(pos==ipAddr.length-1)
        {
            temp.url = url;
            return;
        }
        this.insertUtil(temp, ipAddr, url, pos+1);
}
 
function search(node,ipAddr, pos)
{
    let temp = null;
        if(pos==ipAddr.length-1)  
        {
            temp = node.child.get(ipAddr[pos]);
            if(temp!=null)
                return temp.url;
        }
        if(node.child.has(ipAddr[pos]))
        {  
            temp = node.child.get(ipAddr[pos]);
            return this.search(temp, ipAddr, pos+1);      
        }
        return "No url associated/Invalid IP address";
}
 
let ipAdd = ["107.108.11.123","107.109.123.255", "74.125.200.106"];
let urls = ["www.samsung.com",
                 "www.samsung.net", "www.google.in"];
 
let root = new TrieNode();
insert(root, ipAdd, urls);
//System.out.println(root);
document.write("74.125.200.106 : "+
                   search(root, "74.125.200.106", 0)+"<br>");
document.write("107.109.123.245 : "+
                   search(root, "107.109.123.245", 0)+"<br>");
 
// This code is contributed by patel2127
</script>


C#




using System;
using System.Collections.Generic;
 
public class TrieNode
{
    public Dictionary<char, TrieNode> Child { get; set; }
    public string Url { get; set; }
 
    public TrieNode()
    {
        Child = new Dictionary<char, TrieNode>();
        Url = "";
    }
}
 
public class ReverseDNSLookup
{
    public void Insert(TrieNode node, List<string> ipAdd, List<string> urls)
    {
        for (int i = 0; i < ipAdd.Count; ++i)
        {
            InsertUtil(node, ipAdd[i], urls[i], 0);
        }
    }
 
    public void InsertUtil(TrieNode node, string ipAddr, string url, int pos)
    {
        TrieNode temp = null;
        if (node.Child.ContainsKey(ipAddr[pos]))
        {
            temp = node.Child[ipAddr[pos]];
        }
        else
        {
            temp = new TrieNode();
            node.Child[ipAddr[pos]] = temp;
        }
        if (pos == ipAddr.Length - 1)
        {
            temp.Url = url;
            return;
        }
        InsertUtil(temp, ipAddr, url, pos + 1);
    }
 
    public string Search(TrieNode node, string ipAddr, int pos)
    {
        TrieNode temp = null;
        if (pos == ipAddr.Length - 1)
        {
            if (node.Child.ContainsKey(ipAddr[pos]))
            {
                temp = node.Child[ipAddr[pos]];
                if (!string.IsNullOrEmpty(temp.Url))
                {
                    return temp.Url;
                }
            }
        }
        if (node.Child.ContainsKey(ipAddr[pos]))
        {
            temp = node.Child[ipAddr[pos]];
            return Search(temp, ipAddr, pos + 1);
        }
        return "No url associated/Invalid IP address";
    }
}
 
class Program
{
    static void Main(string[] args)
    {
        ReverseDNSLookup r = new ReverseDNSLookup();
        List<string> ipAdd = new List<string>() { "107.108.11.123", "107.109.123.255", "74.125.200.106" };
        List<string> urls = new List<string>() { "www.samsung.com", "www.samsung.net", "www.google.in" };
 
        TrieNode root = new TrieNode();
        r.Insert(root, ipAdd, urls);
        Console.WriteLine("74.125.200.106 : " + r.Search(root, "74.125.200.106", 0));
        Console.WriteLine("107.109.123.245 : " + r.Search(root, "107.109.123.245", 0));
    }
}


Output: 

74.125.200.106 : www.google.in
107.109.123.245 : No url associated/Invalid IP address

 

Time Complexity:

The insert() method has a time complexity of O(nm), where n is the number of IP addresses and m is the length of the IP address string.
The search() method has a time complexity of O(m), where m is the length of the IP address string.
Space Complexity:

The space complexity of the TrieNode class is O(k * n), where k is the average length of the URL and n is the number of URLs.
The space complexity of the ReverseDNSLookup class is O(1) since it only contains methods and no additional variables.
The overall space complexity of the program is O(k * n), where k is the average length of the URL and n is the number of URLs.

Python3 Implementation: 
 

C++




// C++ Program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Trie Node structure
class TrieNode {
public:
    vector<TrieNode*> child;
    string url;
    bool is_end;
 
    TrieNode() {
        child.resize(11);
        for (int i = 0; i < 11; i++) {
            child[i] = nullptr;
        }
        url = "";
        is_end = false;
    }
};
 
class Trie {
public:
    TrieNode* root;
 
    Trie() {
        root = new TrieNode();
    }
 
    int getIndex(char c) {
        // For the . (dot) in IP address, we'll use the 10th index in child list
        return c == '.' ? 10 : c - '0';
    }
 
    void insert(string ip, string domain) {
        TrieNode* cur = root;
        int n = ip.length();
 
        for (int level = 0; level < n; level++) {
            int idx = getIndex(ip[level]);
 
            if (cur->child[idx] == nullptr) {
                // Create a new trie node if not available for a particular digit
                // and assign to the respective index
                cur->child[idx] = new TrieNode();
            }
 
            cur = cur->child[idx];
        }
        // At the end, we'll map the domain name and mark the end node
        cur->url = domain;
        cur->is_end = true;
    }
 
      // function for search domain
    string search_domain(string ip) {
        TrieNode* cur = root;
        int n = ip.length();
        // We'll use the digits of IP address to form the trie structure
        for (int level = 0; level < n; level++) {
            int idx = getIndex(ip[level]);
            if (cur->child[idx] == nullptr) {
                return "Domain name not found";
            }
 
            cur = cur->child[idx];
        }
        // Returns the url when all the digits in ip found
        if (cur && !cur->url.empty()) {
            return cur->url;
        }
 
        return "Domain name not found";
    }
};
 
// driver code to test above functions
int main() {
    vector<string> ip = {"107.108.11.123", "107.109.123.255", "74.125.200.106"};
    vector<string> domain = {"www.samsung.com", "www.samsung.net", "www.google.co.in"};
 
    Trie trie;
    for(int idx = 0; idx < ip.size(); idx++)
        trie.insert(ip[idx], domain[idx]);
   
    // function call
    cout << trie.search_domain("107.109.123.255") << endl;
    cout << trie.search_domain("107.109.123.245") << endl;
 
    return 0;
}


Java




import java.util.*;
 
// Trie Node structure
class TrieNode {
    Vector<TrieNode> child;
    String url;
    boolean is_end;
 
    TrieNode()
    {
        child = new Vector<TrieNode>(11);
        for (int i = 0; i < 11; i++) {
            child.add(null);
        }
        url = "";
        is_end = false;
    }
}
 
class Trie {
    TrieNode root;
 
    Trie() { root = new TrieNode(); }
 
    int getIndex(char c)
    {
        // For the . (dot) in IP address, we'll use the 10th
        // index in child list
        return c == '.' ? 10 : c - '0';
    }
 
    void insert(String ip, String domain)
    {
        TrieNode cur = root;
        int n = ip.length();
 
        for (int level = 0; level < n; level++) {
            int idx = getIndex(ip.charAt(level));
 
            if (cur.child.get(idx) == null) {
                // Create a new trie node if not available
                // for a particular digit and assign to the
                // respective index
                cur.child.set(idx, new TrieNode());
            }
 
            cur = cur.child.get(idx);
        }
        // At the end, we'll map the domain name and mark
        // the end node
        cur.url = domain;
        cur.is_end = true;
    }
 
    // function for search domain
    String search_domain(String ip)
    {
        TrieNode cur = root;
        int n = ip.length();
        // We'll use the digits of IP address to form the
        // trie structure
        for (int level = 0; level < n; level++) {
            int idx = getIndex(ip.charAt(level));
            if (cur.child.get(idx) == null) {
                return "Domain name not found";
            }
 
            cur = cur.child.get(idx);
        }
        // Returns the url when all the digits in ip found
        if (cur != null && !cur.url.isEmpty()) {
            return cur.url;
        }
 
        return "Domain name not found";
    }
}
 
// driver code to test above functions
public class Main {
    public static void main(String[] args)
    {
        String[] ip = { "107.108.11.123", "107.109.123.255",
                        "74.125.200.106" };
        String[] domain
            = { "www.samsung.com", "www.samsung.net",
                "www.google.co.in" };
 
        Trie trie = new Trie();
        for (int idx = 0; idx < ip.length; idx++)
            trie.insert(ip[idx], domain[idx]);
 
        // function call
        System.out.println(
            trie.search_domain("107.109.123.255"));
        System.out.println(
            trie.search_domain("107.109.123.245"));
    }
}


Python3




# Trie Node
class TrieNode:
    def __init__(self):
        self.child = [None] * 11
        self.url = None
        self.is_end = False
 
class Trie:
    def __init__(self):
        self.root = TrieNode()
     
    def getIndex(self, c):
        # For the . (dot) in IP address, we'll use the 10th index in child list
        return 10 if c == '.' else int(c)
     
    def insert(self, ip, domain):
        cur = self.root
        n = len(ip)
         
        for level in range(n):
            # We'll use the digits of IP address to form the trie structure
            idx = self.getIndex(ip[level])
             
            if cur.child[idx] is None:
                # Create a new trie node if not available for a particular digit
                # and assign to the respective index
                cur.child[idx] = TrieNode()
                 
            cur = cur.child[idx]
         
        # At the end, we'll map the domain name and mark the end node
        cur.url = domain
        cur.is_end = True
     
    def search_domain(self, ip):
        cur = self.root
        n = len(ip)
         
        # Traverse through the trie structure with all digits in ip address
        for level in range(n):
            idx = self.getIndex(ip[level])
            if cur.child[idx] is None:
                return "Domain name not found"
             
            cur = cur.child[idx]
         
        # Returns the url when all the digits in ip found
        if cur and cur.url:
            return cur.url
         
        return "Domain name not found"
 
# Driver Code
ip = ["107.108.11.123", "107.109.123.255", "74.125.200.106"]
domain = ["www.samsung.com", "www.samsung.net", "www.google.co.in"]
 
trie = Trie()
for idx in range(len(ip)):
    trie.insert(ip[idx], domain[idx])
 
print(trie.search_domain("107.109.123.255"))
print(trie.search_domain("107.109.123.245"))
 
# This code is contributed by Abhilash Pujari


Javascript




// Trie Node
class TrieNode {
    constructor() {
        this.child = new Array(11).fill(null);
        this.url = null;
        this.is_end = false;
    }
}
 
class Trie {
    constructor() {
        this.root = new TrieNode();
    }
 
    getIndex(c) {
        // For the . (dot) in IP address, we'll use the 10th index in child list
        return c == '.' ? 10 : parseInt(c);
    }
 
    insert(ip, domain) {
        let cur = this.root;
        let n = ip.length;
 
        for (let level = 0; level < n; level++) {
            let idx = this.getIndex(ip[level]);
 
            if (cur.child[idx] == null) {
                // Create a new trie node if not available for a particular digit
                // and assign to the respective index
                cur.child[idx] = new TrieNode();
            }
 
            cur = cur.child[idx];
        }
        // At the end, we'll map the domain name and mark the end node
        cur.url = domain;
        cur.is_end = true;
    }
 
    search_domain(ip) {
        let cur = this.root;
        let n = ip.length;
        // We'll use the digits of IP address to form the trie structure
        for (let level = 0; level < n; level++) {
            let idx = this.getIndex(ip[level]);
            if (cur.child[idx] == null) {
                return "Domain name not found";
            }
 
            cur = cur.child[idx];
        }
        // Returns the url when all the digits in ip found
        if (cur && cur.url) {
            return cur.url;
        }
 
        return "Domain name not found";
    }
}
 
let ip = ["107.108.11.123", "107.109.123.255", "74.125.200.106"]
let domain = ["www.samsung.com", "www.samsung.net", "www.google.co.in"]
 
let trie = new Trie()
for(let idx = 0; idx<ip.length; idx++)
    trie.insert(ip[idx], domain[idx])
 
console.log(trie.search_domain("107.109.123.255"))
console.log(trie.search_domain("107.109.123.245"))


C#




using System;
 
using System.Collections.Generic;
 
class TrieNode
 
{
 
    public TrieNode[] child;
 
    public string url;
 
    public bool is_end;
 
    public TrieNode()
 
    {
 
        child = new TrieNode[11];
 
        for (int i = 0; i < 11; i++)
 
        {
 
            child[i] = null;
        }
 
        url = "";
 
        is_end = false;
    }
}
 
class Trie
 
{
 
    public TrieNode root;
 
    public Trie() { root = new TrieNode(); }
 
    public int GetIndex(char c)
 
    {
 
        // For the . (dot) in IP address, we'll use the 10th
        // index in child list
 
        return c == '.' ? 10 : c - '0';
    }
 
    public void Insert(string ip, string domain)
 
    {
 
        TrieNode cur = root;
 
        int n = ip.Length;
 
        for (int level = 0; level < n; level++)
 
        {
 
            int idx = GetIndex(ip[level]);
 
            if (cur.child[idx] == null)
 
            {
 
                // Create a new trie node if not available
                // for a particular digit
 
                // and assign to the respective index
 
                cur.child[idx] = new TrieNode();
            }
 
            cur = cur.child[idx];
        }
 
        // At the end, we'll map the domain name and mark
        // the end node
 
        cur.url = domain;
 
        cur.is_end = true;
    }
 
    // function for search domain
 
    public string SearchDomain(string ip)
 
    {
 
        TrieNode cur = root;
 
        int n = ip.Length;
 
        // We'll use the digits of IP address to form the
        // trie structure
 
        for (int level = 0; level < n; level++)
 
        {
 
            int idx = GetIndex(ip[level]);
 
            if (cur.child[idx] == null)
 
            {
 
                return "Domain name not found";
            }
 
            cur = cur.child[idx];
        }
 
        // Returns the url when all the digits in ip found
 
        if (cur != null && !string.IsNullOrEmpty(cur.url))
 
        {
 
            return cur.url;
        }
 
        return "Domain name not found";
    }
}
 
// driver code to test above functions
 
class Program
 
{
 
    static void Main(string[] args)
 
    {
 
        string[] ip = { "107.108.11.123", "107.109.123.255",
                        "74.125.200.106" };
 
        string[] domain
            = { "www.samsung.com", "www.samsung.net",
                "www.google.co.in" };
 
        Trie trie = new Trie();
 
        for (int idx = 0; idx < ip.Length; idx++)
 
            trie.Insert(ip[idx], domain[idx]);
 
        // function call
 
        Console.WriteLine(
            trie.SearchDomain("107.109.123.255"));
 
        Console.WriteLine(
            trie.SearchDomain("107.109.123.245"));
    }
}


Output: 

www.samsung.net
Domain name not found

 

 



Similar Reads

How to Implement Forward DNS Look Up Cache?
We have discussed implementation of Reverse DNS Look Up Cache. Forward DNS look up is getting IP address for a given domain name typed in the web browser. The cache should do the following operations : 1. Add a mapping from URL to IP address 2. Find IP address for a given URL. There are a few changes from reverse DNS look up cache that we need to i
13 min read
Difference between FCFS and C-LOOK disk scheduling algorithm
1. FCFS Disk Scheduling Algorithm FCFS stands for First Come First Serve, this algorithm entertains the task in the order they arrived in the disk queue. It is the simplest and easy to understand disk scheduling algorithm. In this, the head or pointer moves in the direction in which the task arrives and moves till all request is served. However, th
3 min read
Difference between C-LOOK and C-SCAN Disk Scheduling Algorithm
C-LOOK Disk Scheduling Algorithm C-LOOK is the modified version of both LOOK and C-scan algorithms. In this algorithm, the head starts from the first request in one direction and moves towards the last request at another end, serving all request in between. After reaching the last request in one end, the head jumps in another direction and move tow
3 min read
Compute the parity of a number using XOR and table look-up
Parity of a number refers to whether it contains an odd or even number of 1-bits. The number has “odd parity”, if it contains odd number of 1-bits and is “even parity” if it contains even number of 1-bits. 1 --&gt; parity of the set is odd 0 --&gt; parity of the set is even Examples: Input : 254 Output : Odd Parity Explanation : Binary of 254 is 11
9 min read
Look-and-Say Sequence
Find the n'th term in Look-and-say (Or Count and Say) Sequence. The look-and-say sequence is the sequence of the below integers: 1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211, ... How is the above sequence generated? n'th term is generated by reading (n-1)'th term. The first term is "1"Second term is "11", generated by reading first term as
15+ min read
Implement reverse AnimatedVectorDrawables using MorphView in Android
MorphView is a convenience library for reverse AnimatedVectorDrawables. The AnimatedVectorDrawables The class was introduced in API 21 and is used to animate Vector Drawables beautifully and easily. Following are some of the things that can perform with AnimatedVectorDrawable: Rotate, Scale, Translate VectorDrawables Animate the VectorDrawable such
5 min read
Count of unique pairs (i, j) in an array such that sum of A[i] and reverse of A[j] is equal to sum of reverse of A[i] and A[j]
Given an array arr[] consisting of N positive integers, the task is to find the count of unique pairs (i, j) such that the sum of arr[i] and the reverse(arr[j]) is the same as the sum of reverse(arr[i]) and arr[j]. Examples: Input: arr[] = {2, 15, 11, 7}Output: 3Explanation:The pairs are (0, 2), (0, 3) and (2, 3). (0, 2): arr[0] + reverse(arr[2]) (
7 min read
Node.js DNS
Node.js DNS (Domain Name System) module provides methods for performing DNS lookups and working with domain names. It allows you to resolve domain names into IP addresses and vice versa, which is essential for network communications and server management. DNS Module in Node.jsThe DNS module in Node.js is a core module that facilitates DNS-related o
3 min read
LRU Cache in Python using OrderedDict
LRU (Least Recently Used) Cache discards the least recently used items first. This algorithm requires keeping track of what was used when, which is expensive if one wants to make sure the algorithm always discards the least recently used item. General implementations of this technique require keeping "age bits" for cache-lines and track the "Least
4 min read
Why Arrays have better cache locality than Linked list?
What is a Cache locality?Cache locality refers to the property of computer programs where they tend to access a small, specific set of data repeatedly in a short period of time. By keeping this frequently accessed data in a small, fast memory region called a cache, the program can speed up its execution time. This is because accessing data from the
9 min read
Least Frequently Used (LFU) Cache Implementation
Least Frequently Used (LFU) is a caching algorithm in which the least frequently used cache block is removed whenever the cache is overflowed. In LFU we check the old page as well as the frequency of that page and if the frequency of the page is larger than the old page we cannot remove it and if all the old pages are having same frequency then tak
9 min read
Cache Oblivious Algorithm
Cache oblivious is a way of achieving algorithms that are efficient in arbitrary memory hierarchies without the use of complicated multi-level memory models. Cache oblivious algorithms are algorithms that use asymptotically optimal amounts of work, move data asymptotically optimally among multiple levels of cache, and indirectly use processor cache
14 min read
Cache oblivious kd-Tree
Cache-oblivious kd-tree data structures are a great utility that performs multi-dimensional orthogonal range searching. One of the salient terms of kd trees is binary space partitioning which periodically subdivides space into two convex sets by using hyperplanes as splitting. This article focuses on discussing the following topics in detail- Cache
9 min read
Design a data structure for LRU Cache
Design a data structure for LRU Cache. It should support the following operations: get and put. get(key) - Returns the value of the given key if it exists in the cache; otherwise, returns -1.put(key, value) - Inserts or updates the key-value pair in the cache. If the cache reaches capacity, remove the least recently used item before adding the new
5 min read
How to efficiently implement k Queues in a single array?
Introduction : One efficient way to implement k queues in a single array is to use a technique called "circular array implementation of k queues." This approach uses a single array to store elements for all k queues, and it divides the array into k segments, one for each queue. To implement this approach, we need to keep track of two pointers for e
15+ min read
How to implement decrease key or change key in Binary Search Tree?
Given a Binary Search Tree, write a function that takes the following three as arguments: Root of tree Old key value New Key Value The function should change old key value to new key value. The function may assume that old key-value always exists in Binary Search Tree. Example: Input: Root of below tree 50 / \ 30 70 / \ / \ 20 40 60 80 Old key valu
15+ min read
C++ Program to implement Symbol Table
Prerequisite: Symbol Table A Symbol table is a data structure used by the compiler, where each identifier in program's source code is stored along with information associated with it relating to its declaration. It stores identifier as well as it's associated attributes like scope, type, line-number of occurrence, etc. Symbol table can be implement
4 min read
Program to implement Simpson's 3/8 rule
Write a program to implement Simpson's 3/8 rule.The Simpson's 3/8 rule was developed by Thomas Simpson. This method is used for performing numerical integrations. This method is generally used for numerical approximation of definite integrals. Here, parabolas are used to approximate each part of curve.Simpson's 3/8 formula :[Tex]\int_{a}^{b} f(x) d
6 min read
Implement random-0-6-Generator using the given random-0-1-Generator
Given a function random01Generator() that gives you randomly either 0 or 1, implement a function that utilizes this function and generate numbers between 0 and 6(both inclusive). All numbers should have same probabilities of occurrence. Examples: on multiple runs, it gives 3 2 3 6 0 Approach : The idea here is to find the range of the smallest numb
5 min read
Implement dynamic deque using templates class and a circular array
The task is to implement a dynamic Deque using templates class and a circular array, having the following functionalities: front(): Get the front item from the deque.back(): Get the last item from the deque.push_back(X): Push X at the end of the deque.push_front(X): Push X at the start of the deque.pop_front(): Delete an element from the start of t
9 min read
Program to implement Inverse Interpolation using Lagrange Formula
Given task is to find the value of x for a given y of an unknown function y = f(x) where values of some points (x, y) pairs are given.Let, y = f(x) be an unknown function where x in an independent variable. For different values of x, say [Tex]x_0, x_1, x_2, ..., x_m ( i.e [/Tex][Tex]x_k, k=0, 1, 2, 3...m) [/Tex]values of respective [Tex]y_0, y_1, y
8 min read
Implement a Dictionary using Trie
Implement a dictionary using Trie such that if the input is a string representing a word, the program prints its meaning from the prebuilt dictionary. Examples: Input: str = "map" Output: a diagrammatic representation of an area Input: str = "language" Output: the method of human communication Approach: We can use a Trie to efficiently store string
9 min read
How to implement our own Dynamic Array class in Java?
Given task is to implement a class in Java which behaves just like the Dynamic array using ArrayList. ArrayList is same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. ArrayList elements are placed in contiguous storage so that
4 min read
Implement Undo and Redo features of a Text Editor
Given an array of strings Q[], consisting of queries of the following types: “WRITE X”: Write a character X into the document.“UNDO”: Erases the last change made to the document.“REDO”: Restores the most recent UNDO operation performed on the document.“READ”: Reads and prints the contents of the documents. Examples: Input: Q = {“WRITE A”, “WRITE B”
10 min read
Top 12 Data Structure Algorithms to Implement in Practical Applications in 2021
New Year...New Beginning...!!! What's your plan for this year??? (Being a programmer) Of course, if you're a programmer then this year you will also write the code, build the projects and you will solve a lot of coding questions. Let's talk about Data Structures and Algorithms... Heart of computer science and the programmer's breath to live in the
14 min read
Implement dynamic queue using templates class and a circular array
In this article, we will discuss how to create a dynamic circular queue using a circular array having the following functionality: Front(): Get the front item from the queue.Back(): Get the last item from the queue.Push(X): Push the X in the queue at the end of the queue.Pop(): Delete an element from the queue. Below is the step-by-step illustratio
7 min read
How to implement a Stack using list in C++ STL
In this article, we will discuss how to implement a Stack using list in C++ STL. Stack is a linear data structure which follows. LIFO(Last In First Out) or FILO(First In Last Out). It mainly supports 4 major operations:1. Push: Push an element into the stack.2. Pop: Removes the element by following the LIFO order.3. Top: Returns the element present
3 min read
C# Program to Implement the Same Method in Multiple Classes
C# is a general-purpose programming language it is used to create mobile apps, desktop apps, websites, and games. In C#, an object is a real-world entity. Or in other words, an object is a runtime entity that is created at runtime. It is an instance of a class. In this article, Implement the same method in Multiple Classes The implementation of the
3 min read
Java Program to Implement Commentz-Walter Algorithm
The Commentz-Walter algorithm is a string-matching algorithm that is used to search for a given pattern in a text string. It is a variant of the Knuth-Morris-Pratt (KMP) algorithm, which is a well-known algorithm for string matching. This algorithm works by preprocessing the pattern string to create a failure function that encodes the information a
6 min read
How to implement Stack and Queue using ArrayDeque in Java
ArrayDeque in Java The ArrayDeque in Java provides a way to apply resizable-array in addition to the implementation of the Deque interface. It is also known as Array Double Ended Queue or Array Deck. This is a special kind of array that grows and allows users to add or remove an element from both sides of the queue. ArrayDeque class implements Queu
6 min read
Article Tags :
Practice Tags :