Category: HashTable

Count of substrings with the frequency of at most one character as Odd

Given a string S of N characters, the task is to calculate the total number of non-empty substrings such that at most one character occurs an odd number of times.Example: Input: S = “aba”Output: 4Explanation: The valid substrings are “a”, “b”, “a”, and “aba”. Therefore, the total number of required substrings are 4.Input: “aabb”Output: 9Explanation: The […]

Continue Reading

Program to implement Hash Table using Open Addressing

#include #include   struct HashNode {    int key;    int value;};  const int capacity = 20;int size = 0;  struct HashNode** arr;struct HashNode* dummy;  void insert(int key, int V){      struct HashNode* temp        = (struct HashNode*)malloc(sizeof(struct HashNode));    temp->key = key;    temp->value = V;              int hashIndex = key % capacity;          while (arr[hashIndex] != NULL           && arr[hashIndex]->key != key           && arr[hashIndex]->key != -1) {        hashIndex++;        hashIndex %= capacity;    }              if (arr[hashIndex] == NULL        || arr[hashIndex]->key == -1)        size++;      arr[hashIndex] = […]

Continue Reading

Number of substrings having an equal number of lowercase and uppercase letters

Number of substrings having an equal number of lowercase and uppercase lettersGiven string S consisting of lowercase and uppercase letters, the task is to find the number of substrings having the equal number of lowercase and uppercase letters.Examples:Input: S = “gEEk”Output: 3Explanation:Following are the substrings having the equal number of lowercase and uppercase letters:“gE”“gEEk”“Ek”Therefore, the […]

Continue Reading

Convert a number to another by dividing by its factor or removing first occurrence of a digit from an array

  #include using namespace std;  int isPresent(int n, int x){        string num = to_string(n);          for (int i = 0; i < num.size();         i++) {                          if ((num[i] - '0') == x)            return i;    }    return -1;}  int removeDigit(int n, int index){        string num = to_string(n);          string ans = "";          for (int i = 0;         i < num.size(); i++) {        if (i != index)            ans += num[i];    }              if (ans == "" || (ans.size() == […]

Continue Reading

Count number of substrings having at least K distinct characters

Count number of substrings having at least K distinct characters Given a string S consisting of N characters and a positive integer K, the task is to count the number of substrings having at least K distinct characters.Examples:Input: S = “abcca”, K = 3Output: 4Explanation:The substrings that contain at least K(= 3) distinct characters are:“abc”: […]

Continue Reading

Check if every node can me made accessible from a node of a Tree by at most N/2 given operations

import java.io.*;import java.util.HashMap;  class GFG {                  public static void    findNode(HashMap map,             int n)    {                          int[] a = new int[n];          for (int i = 0; i < n; i++) {            a[i] = map.getOrDefault(i + 1, 0);        }                          int count0 = 0;                  for (int i = 0; i < n; i++) {                                      if (a[i] == 0) {                                  count0++;            }        }          count0 -= 1;                          if (count0            

Continue Reading