Count Occurrences of Anagrams in C++

C++Server Side ProgrammingProgramming

We are given an input as a text stream and a word, and the task is to find out the count of occurrences of anagrams of the word in the given text stream. Anagrams are generated by rearranging letters from a word which ends up being a different word or phrase like anagrams of words in a statement "New York Times" can be formed as "Monkeys write".

For Example

Input: String string-:  “workitwrokoffowkr” word = “work”   

Output: Count of occurrences of anagram in the string are: 3

Explanation: The possible anagrams for the word “work” are work,wrok, rowk, owkr, etc. In the given string the available anagrams for work are work, wrok and owkr therefore the count is 3.

Input: String string-:  “expresshycool” word = “Zen”   

Output: Count of occurrences of anagram in the string are: 0

Explanation: The possible anagrams for the word “zen” are nez, ezn, enz, zne, nze, zen, etc. In the given string there are no available anagrams for the word “zen” therefore the count is 0.

Approach used in the below program is as follows

  • We are given a character string(stream) and word(w) which is passed in the function countAna(stream,w) for further processing.
  • Inside the function(countAna) a count flag is initialized
  • Start a FOR loop from i=0 to i <= (stream.length()) - (w.length()) .
  • Inside the function(countAna) call the function arrangeAna(w, stream.substring(i, i + (w.length()))) passing the word and stream.substring(i, i + (w.length())) which generates substrings (of length equal to the word) from the stream.
  • Inside the function arrangeAna the strings are converted to character array and are sorted
  • (Arrays.equals(c1, c2)) then checks if the generated substring is equal to the word and the   response(true/false) is sent accordingly.
  • The count flag (inside the countAna()) is then incremented if the response is true.
  • The response is then captured in the main() method and the output is printed.

Example

import java.io.*;
import java.util.*;

public class testClass {
   static boolean arrangeAna(String s1, String s2) {
      char[] c1 = s1.toCharArray();
      char[] c2 = s2.toCharArray();
      Arrays.sort(c1);
      Arrays.sort(c2);
      if (Arrays.equals(c1, c2)) {
         return true;
      } else {
         return false;
      }
   }
   static int countAna(String stream, String w) {
      int count = 0;
      for (int i = 0; i <= (stream.length()) - (w.length()); i++) {
         if (arrangeAna(w, stream.substring(i, i + (w.length())))) {
            count++;
         }
      }
      return count;
   }

   public static void main(String args[]) {
      Scanner scan = new Scanner(System.in);
      String stream = scan.next(); //workitwrokoffowkr
      String w = scan.next(); //work
      System.out.print(countAna(stream, w));
   }
}

If we run the above code it will generate the following output −

Output

Count of occurrences of anagram in the string are: 3
raja
Published on 29-Jan-2021 12:51:13
Advertisements