Saturday, August 25, 2018

Sherlock and Anagrams

Two strings are anagrams of each other if the letters of one string can be rearranged to form the other string. Given a string, find the number of pairs of substrings of the string that are anagrams of each other.
For example , the list of all anagrammatic pairs is  at positions  respectively.
Function Description
Complete the function sherlockAndAnagrams in the editor below. It must return an integer that represents the number of anagrammatic pairs of substrings in .
sherlockAndAnagrams has the following parameter(s):
  • s: a string .
Input Format
The first line contains an integer , the number of queries.
Each of the next  lines contains a string  to analyze.
Constraints


String  contains only lowercase letters  ascii[a-z].
Output Format
For each query, return the number of unordered anagrammatic pairs.
Solution :- 

  static int sherlockAndAnagrams(string s)
        {            
            int count = 0;
            var dictStr = new Dictionary<string, int>();
            for (int i = 0; i < s.Length; i++)
            {
                for (int j=i+1; j <= s.Length; j++)
                {
                    var str = s.Substring(i, j-i);
                    var chars = str.ToCharArray();
                    Array.Sort(chars);
                    var word = new string(chars);
                    if (dictStr.ContainsKey(word))
                    {
                        var value = dictStr[word];
                        count = count + value;
                        dictStr[word] = value + 1;
                    }
                    else
                        dictStr.Add(word, 1);                    
                }
            }
            return count;
        }


Thursday, August 23, 2018

Linked Lists: Detect a Cycle

Check out the resources on the page's right side to learn more about linked lists. The video tutorial is by Gayle Laakmann McDowell, author of the best-selling interview book Cracking the Coding Interview.
A linked list is said to contain a cycle if any node is visited more than once while traversing the list. For example, in the following graph there is a cycle formed when node  points back to node .
image
Function Description
Complete the function has_cycle in the editor below. It must return a boolean true if the graph contains a cycle, or false.
has_cycle has the following parameter(s):
  • : a pointer to a Node object that points to the head of a linked list.
Note: If the list is empty,  will be null.
Input Format
There is no input for this challenge. A random linked list is generated at runtime and passed to your function.
Constraints
Output Format
If the list contains a cycle, your function must return true. If the list does not contain a cycle, it must return false. The binary integer corresponding to the boolean value returned by your function is printed to stdout by our hidden code checker.
Solution :-
boolean hasCycle(Node head) {

    HashSet<Node> strObjMap = new HashSet<Node>();
    while(head != null)
    {
        if(!strObjMap.contains(head))
        {
            strObjMap.add(head);
        }
        else
        {
            return true;
        }
        head = head.next;
    }
    return false;
}


Reverse a doubly linked list

You’re given the pointer to the head node of a doubly linked list. Reverse the order of the nodes in the list. The head node might be NULL to indicate that the list is empty. Change the next and prev pointers of all the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list.
Function Description
Complete the reverse function in the editor below. It should return a reference to the head of your reversed list.
reverse has the following parameter(s):
  • head: a reference to the head of a DoublyLinkedList
Input Format
The first line contains an integer , the number of test cases.
Each test case is of the following format:
  • The first line contains an integer , the number of elements in the linked list.
  • The next  lines contain an integer each denoting an element of the linked list.
Constraints
Output Format
Return a reference to the head of your reversed list. The provided code will print the reverse array as a one line of space-separated integers for each test case.
Solution :-
   static DoublyLinkedListNode reverse(DoublyLinkedListNode head) {
 if (head == null)
                return head;
            var temp = head.next;
            head.next = head.prev;
            head.prev = temp;
            if (head.prev == null)
                return head;
            return reverse(head.prev);
    }




Kafka setup in window

Here's a step-by-step guide on how to do it : 1. Prerequisites : Before you begin, make sure you have the following prerequisites inst...