-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask5.java
More file actions
26 lines (26 loc) · 800 Bytes
/
Copy pathtask5.java
File metadata and controls
26 lines (26 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.Arrays;
import java.util.Scanner;
public class AnagramCount {
// Function to sort characters of a string
static String sortString(String str) {
char[] charArray = str.toCharArray();
Arrays.sort(charArray);
return new String(charArray);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the text: ");
String text = sc.nextLine();
System.out.print("Enter the pattern: ");
String pattern = sc.nextLine();
int k = pattern.length();
int count = 0;
String sortedPattern = sortString(pattern);
for (int i = 0; i <= text.length() - k; i++) {
String substring = text.substring(i, i + k);
if (sortString(substring).equals(sortedPattern)) {
count++;
}
System.out.println("Number of anagram substrings: " + count);
}
}