Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Anagram1{
- public static boolean check(char[] c1,char[] c2,int l1,int l2){
- /*if the lengths of the strings are not equal,they cannot be anagrams of each other */
- if(l1!=l2){
- return false;
- }
- int count = 0;
- //nested for loops to compare the characters of both strings.
- //if the characters match,then the strings are anagrams of each other.
- for(int i=0; i<l1; i++){
- for(int j=0; j<l2; j++){
- if(c1[i]==c2[j]){
- count++;
- break; //"break" used so as to count only a single occurrence of a
- } //character.
- }
- }
- if(count==l1 && count==l2){
- return true;
- }
- return false;
- }
- public static void main(String[] args) {
- String s1 = "abcd";
- String s2 = "acdb";
- char[] c1 = s1.toCharArray();
- char[] c2 = s2.toCharArray();
- int l1 = s1.length();
- int l2 = s2.length();
- System.out.println(check(c1,c2,l1,l2));
- }
- }
- String s1 = "abbd";
- String s2 = "acdb";
- import java.util.ArrayList;
- import java.util.Collections;
- public class Anagram1 {
- public static void main(String[] args) {
- String s1 = "abcd";
- String s2 = "adbc";
- System.out.println(check(s1, s2));
- }
- public static boolean check(String s1, String s2) {
- // Remove white space and change to lower case.
- s1 = s1.replace(" ","").toLowerCase();
- s2 = s2.replace(" ","").toLowerCase();
- // If they are equal, return true. If they are not the same length,
- // return false.
- if (s1.equals(s2)) {
- return true;
- } else if (s1.length() != s2.length()) {
- return false;
- }
- // Convert to ArrayList for sorting.
- ArrayList<Character> sl1 = new ArrayList<Character>();
- ArrayList<Character> sl2 = new ArrayList<Character>();
- for (char ch1 : s1.toCharArray()) {
- sl1.add(ch1);
- }
- for (char ch2 : s2.toCharArray()) {
- sl2.add(ch2);
- }
- // Sort both ArrayLists
- Collections.sort(sl1);
- Collections.sort(sl2);
- // If they are equal return true, else return false.
- if (sl1.equals(sl2)) {
- return true;
- } else {
- return false;
- }
- }
- }
- if(count==l1 && count==l2){
- return true;
- }
- public static void main(String[]args){
- //Diff lengths = false
- System.out.println(check("acd", "bacd"));
- //Same letters, mixed = true
- System.out.println(check("abcd", "bacd"));
- //Same string = true
- System.out.println(check("abcd", "abcd"));
- //Diff letters = false
- System.out.println(check("abcd", "gacd"));
- //Same characters = true
- System.out.println(check("abcd123$", "1b2a3c$d"));
- //Multiple occurances of a letter = true
- System.out.println(check("aaacddbg", "acadadgb"));
- }
- public static boolean check(String c1, String c2){
- if(c1.length() != c2.length()){
- return false;
- }
- char[] word1 = c1.toCharArray();
- char[] word2 = c2.toCharArray();
- Map<Character, Integer> map1 = new HashMap<>();
- Map<Character, Integer> map2 = new HashMap<>();
- for(int i = 0; i < word1.length; i++){
- map1.put(word1[i], (map1.get(word1[i]) == null) ? 1 : map1.get(word1[i])+1);
- }
- for(int i = 0; i < word2.length; i++){
- map2.put(word2[i], (map2.get(word2[i]) == null) ? 1 : map2.get(word2[i])+1);
- }
- if(map1.equals(map2)){
- return true;
- }else{
- return false;
- }
- }
- public class Anagram{
- public static boolean check(char[] c1,char[] c2,int l1,int l2){
- /*If the lengths are unequal,return false.*/
- if(l1!=l2){
- return false;
- }
- int[] count1 = new int[256];
- int[] count2 = new int[256];
- for(int i=0; i<l1; i++){
- count1[c1[i]]++;
- }
- for(int j=0; j<l2; j++){
- count2[c2[j]]++;
- }
- /*Comparing both the count arrays.*/
- for(int k=0; k<count1.length; k++){
- if(count1[k]!=count2[k]){
- return false;
- }
- }
- return true;
- }
- public static void main(String[] args) {
- String s1 = "anagram";
- String s2 = "nagarnm";
- char[] ch1 = s1.toCharArray();
- char[] ch2 = s2.toCharArray();
- int len1 = s1.length();
- int len2 = s2.length();
- System.out.println(check(ch1,ch2,len1,len2));
- }
- }
Add Comment
Please, Sign In to add comment