Category: HashSet

Length of the longest substring consisting only of vowels in non-increasing order

import java.io.*;import java.util.*;  class GFG {                  static int count(String S, int N)    {                  char ch[] = { ‘u’, ‘o’, ‘i’, ‘e’, ‘a’ };                  int j = 0;                  int res = 0;                          int count = 0;                  HashSet mp = new HashSet();                  for (int i = 0; i < N; ++i) {                          if (S.charAt(i) == ch[j]) {                                  ++count;                                  mp.add(S.charAt(i));                                  if (mp.size() == 5) {                    res = Math.max(res, count);                }            }                                      else if (j + […]

Continue Reading

Numbers of pairs from an array whose average is also present in the array

  import java.io.*;import java.util.*;  class GFG {              static int getCountPairs(int arr[],                             int N, int S)    {        HashMap mp            = new HashMap();                          for (int i = 0; i < N; i++) {                                      if (!mp.containsKey(arr[i]))                mp.put(arr[i], 0);              mp.put(arr[i],                   mp.get(arr[i]) + 1);        }                          int twice_count = 0;                          for (int i = 0; i < N; i++) {                                      if (mp.get(S - arr[i])                != null) {                                  twice_count += mp.get(                    S - arr[i]);            }              if (S - arr[i] == arr[i])                twice_count--;        }                  return twice_count / […]

Continue Reading

Count new pairs of strings that can be obtained by swapping first characters of pairs of strings from given array

  #include using namespace std;  void countStringPairs(string a[], int n){          int ans = 0;              for (int i = 0; i < n; i++) {        for (int j = i + 1; j < n; j++) {                                      string p = a[i], q = a[j];                          if (p[0] != q[0]) {                  swap(p[0], q[0]);                int flag1 = 0;                int flag2 = 0;                                                  for (int k = 0; k < n; […]

Continue Reading