how to check how many anagrams a word has in java
Java
// An efficient Java program to count anagrams of a
// pattern in a text.
import java.io.*;
import java.util.*;
public class GFG {
final static int MAX_CHAR = 256
// Function to find if two strings are equal
static boolean isCountZero(int[] count)
{
for (int i = 0; i < MAX_CHAR; i++)
if (count[i] != 0)
return false;
return true;
}
static int countAnagrams(String text, String word)
{
int N = text.length();
int n = word.length();
// Check for first window. The idea is to
// use single count array to match counts
int[] count = new int[MAX_CHAR];
for (int i = 0; i < n; i++)
count[word.charAt(i)]++;
for (int i = 0; i < n; i++)
count[text.charAt(i)]--;
// If first window itself is anagram
int res = 0;
if (isCountZero(count))
res++;
for (int i = n; i < N; i++) {
// Add last character of current
// window
count[text.charAt(i)]--;
// Remove first character of previous
// window.
count[text.charAt(i - n)]++;
// If count array is 0, we found an
// anagram.
if (isCountZero(count))
res++;
}
return res;
}
// Driver code
public static void main(String args[])
{
String text = "";
String word = "for";
System.out.print(countAnagrams(text, word));
}
}
Also in Java:
- Title
- java last element in array
- Category
- Java
- Title
- java dictionary
- Category
- Java
- Title
- how to use lambda in java
- Category
- Java
- Title
- how to input in java
- Category
- Java
- Title
- find a substring in a string java
- Category
- Java
- Title
- java add a list to a list
- Category
- Java
- Title
- java return new instance of generic type
- Category
- Java
- Title
- how to open a text file in java
- Category
- Java
- Title
- prendere valore da tastiera java
- Category
- Java
- Title
- how to append to an array in java
- Category
- Java
- Title
- java file class
- Category
- Java
- Title
- java read file bufferedreader
- Category
- Java
- Title
- java create directory
- Category
- Java
- Title
- how to replace in java
- Category
- Java
- Title
- print out value java
- Category
- Java
- Title
- how to read in a file in java
- Category
- Java
- Title
- deleting elements of an array in java
- Category
- Java
- Title
- how to declare list of object in java as constant
- Category
- Java
- Title
- java create txt file
- Category
- Java
- Title
- python vs java
- Category
- Java
- Title
- get current day java
- Category
- Java
- Title
- java 8 loop in map
- Category
- Java
- Title
- usaco 2018 january contest
- Category
- Java
- Title
- open an existing excel file in java apache poi
- Category
- Java
- Title
- javafx action event enter key
- Category
- Java
- Title
- install java debian 8
- Category
- Java
- Title
- java creare costante
- Category
- Java
- Title
- java 8 iterating and manipulating list
- Category
- Java
- Title
- convert int array to integer list java
- Category
- Java
- Title
- java sql question mark
- Category
- Java
- Title
- java initialize float to zero
- Category
- Java
- Title
- parallel sorting in java 8
- Category
- Java
- Title
- remove extra blank spaces from string in java
- Category
- Java
- Title
- java fullscreen jframe
- Category
- Java
- Title
- java double 2 decimal
- Category
- Java
- Title
- how to add an image to a gui in java windowbuilder
- Category
- Java
- Title
- sending a excel in an attachment in email java
- Category
- Java
- Title
- throwing exceptions java
- Category
- Java
- Title
- java 8 string to localdate
- Category
- Java
- Title
- java for
- Category
- Java
- Title
- convert java list to array
- Category
- Java
- Title
- import java.util.hashset
- Category
- Java
- Title
- how to import jframe in java
- Category
- Java
- Title
- get world by name bukkit
- Category
- Java
- Title
- javadoc reference general toString
- Category
- Java
- Title
- use regex in if statement java
- Category
- Java
- Title
- ujava saum of positive integers
- Category
- Java
- Title
- reverse string using recursion java with explanation
- Category
- Java
- Title
- set iteration java
- Category
- Java
- Title
- hello world java
- Category
- Java
- Title
- java remove non numbers from string
- Category
- Java
- Title
- using class in java
- Category
- Java
- Title
- java square a number
- Category
- Java
- Title
- split method in java
- Category
- Java
- Title
- java for each
- Category
- Java
- Title
- run java classpath command line
- Category
- Java
- Title
- java execute jar from main
- Category
- Java
- Title
- java how to print a newline
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- java pass arraylist by value
- Category
- Java
- Title
- how to get all of the chldren of a panel in java
- Category
- Java
- Title
- how to create a method java
- Category
- Java
- Title
- java break in foreach
- Category
- Java
- Title
- processing library cassette
- Category
- Java
- Title
- html redirect
- Category
- Java
- Title
- how to iterate hashmap in java
- Category
- Java
- Title
- generic method lambda java
- Category
- Java
- Title
- how to add objects in array java
- Category
- Java
- Title
- how to convert a jsonobject to a dbobject
- Category
- Java
- Title
- fibonacci sequence java
- Category
- Java
- Title
- java script to detect and launch all browsers
- Category
- Java
- Title
- basic java coding
- Category
- Java
- Title
- covariant type in java
- Category
- Java
- Title
- math minimum java
- Category
- Java
- Title
- kotlin add element to array
- Category
- Java
- Title
- programmation android avoir acces à la liste des intents de partage
- Category
- Java
- Title
- spigot spawn firework
- Category
- Java
- Title
- java new string with values
- Category
- Java
- Title
- libgdx move camera with touch
- Category
- Java
- Title
- how to create gravity in java
- Category
- Java
- Title
- abstract class in java
- Category
- Java
- Title
- how to draw a line in java
- Category
- Java
- Title
- hibernate Unknown integral data type for ids : java.lang.String
- Category
- Java
- Title
- get value textfield java
- Category
- Java
- Title
- print in one line in java
- Category
- Java
- Title
- capacitor-android. Unfortunately you can't have non-Gradle Java modules and Android-Gradle modules in one project
- Category
- Java
- Title
- check if string is null or empty java
- Category
- Java
- Title
- how to make stage dragable in javafx
- Category
- Java
- Title
- java write arraylist of objects to file
- Category
- Java
- Title
- convert char to string java
- Category
- Java
- Title
- que es un length en java
- Category
- Java
- Title
- cannot lock java compile cache as it has already been locked by this process
- Category
- Java
- Title
- java http interceptor
- Category
- Java
- Title
- java merge sort
- Category
- Java
- Title
- how to count an replace string in java
- Category
- Java
- Title
- how to byheart faster
- Category
- Java
- Title
- download spring
- Category
- Java
- Title
- spring-boot actuator not working
- Category
- Java
- Title
- Don't use a line-beased input after a token-based input.
- Category
- Java
- Title
- bracket balancing program in java
- Category
- Java