Pages

Verify strings have same occurrences of characters in java

Verify strings have same occurrences of characters

  • Program to verify given strings have same number of occurrences of characters.
import java.util.Arrays;

public class VerifyOccurencesInStrings {
public static void main(String[] args) {
String str1 = "keep good";
String str2 = "peek doog";

char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();

Arrays.sort(arr1);
Arrays.sort(arr2);
System.out.println("sored arrays=");
System.out.println(arr1);
System.out.println(arr2);

System.out.println(Arrays.equals(arr1, arr2));
}
}
Output:
Verify strings have same occurrences of characters in java
strings have same occurrences of characters in java

Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.