Sherlock and Anagrams

Sort by

recency

|

1687 Discussions

|

  • + 0 comments

    Java 8

    My solution :

     public static int sherlockAndAnagrams(String s) {
        
           Map<String,Integer> matchMap = new HashMap<>();
          
           for(int i= 0; i < s.length(); i++){
               for(int j= i+1; j <= s.length(); j++){
                    char[] array = s.substring(i, j).toCharArray();
                    Arrays.sort(array);
                    String orderSubString = String.valueOf(array);
                    matchMap.compute(orderSubString, (k,v) -> (v != null ? v : 0) + 1); 
               }
           }
           
           matchMap.replaceAll((k, v) -> IntStream.range(1, v).sum());
           return matchMap.values().stream().reduce(0, (a,b)-> a+b);
        }
    
  • + 0 comments

    C++

    My solution using combinations.

    int get_two_combination(int substr_count)
    {
        if(substr_count < 2)
        {
            return 0;
        }
        return substr_count * (substr_count-1) / 2;
    }
    
    int sherlockAndAnagrams(string s) 
    {
        map<string, int> anagrams;
        int pair_count = 0;
        for(int i = 1; i < s.size(); i++)
        {
            for(int j = 0; (j + i) <= s.size(); j++)
            {
                string cur_substr = s.substr(j, i);
                sort(cur_substr.begin(), cur_substr.end());
                anagrams[cur_substr]++;
                cout << cur_substr << endl;
            }
        }
        
        for(auto it = anagrams.begin(); it != anagrams.end(); it++)
        {
            pair_count += get_two_combination(it->second);
        }
        return pair_count;
    }
    
  • + 0 comments

    java8

    import java.io.*;
    import java.math.*;
    import java.security.*;
    import java.text.*;
    import java.util.*;
    import java.util.concurrent.*;
    import java.util.regex.*;
    
    /**
     * Sherlock and Anagrams
     * Taushree Sarkar
     */
    
    
    public class Solution {
        public static final int ALPHABET_CNT = 26;
    
        static boolean isAnagrams(String s1, String s2) {
    
            char[] chCnt1 = new char[ALPHABET_CNT];
            char[] chCnt2 = new char[ALPHABET_CNT];
    
    
            for (int i = 0, n = s1.length(); i < n; i++) {
                chCnt1[s1.charAt(i) - 97] += 1;
                chCnt2[s2.charAt(i) - 97] += 1;
            }
    
            for (int i = 0; i < ALPHABET_CNT; i++) {
                if (chCnt1[i] != chCnt2[i]) {
                    return false;
                }
            }
    
            return true;
        }
        static int sherlockAndAnagrams(String s) {
            int cnt = 0;
            for (int i = 1, n = s.length(); i < n; i++) {
                List<String> subsetList = new ArrayList<>();
                for (int j = 0; j < n; j++) {
                    if (i + j <= n) {
                        subsetList.add(s.substring(j, i + j));
                    }
                }
    
                for (int k = 0, size = subsetList.size(); k < size; k++) {
                    for (int l = k + 1; l < size; l++) {
                        if (isAnagrams(subsetList.get(k), subsetList.get(l))) {
                            cnt++;
                        }
                    }
                }
            }
    
    
            return cnt;
        }
    
        private static final Scanner scanner = new Scanner(System.in);
    
        public static void main(String[] args) throws IOException {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
    
            int q = scanner.nextInt();
            scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    
            for (int qItr = 0; qItr < q; qItr++) {
                String s = scanner.nextLine();
    
                int result = sherlockAndAnagrams(s);
    
                bufferedWriter.write(String.valueOf(result));
                bufferedWriter.newLine();
            }
    
            bufferedWriter.close();
    
            scanner.close();
        }
    }
    
  • + 0 comments
    import java.io.*;
    import java.math.*;
    import java.security.*;
    import java.text.*;
    import java.util.*;
    import java.util.concurrent.*;
    import java.util.regex.*;
    
    /**
     * Sherlock and Anagrams
     * Taushree Sarkar
     */
    
    
    public class Solution {
        public static final int ALPHABET_CNT = 26;
    
        static boolean isAnagrams(String s1, String s2) {
    
            char[] chCnt1 = new char[ALPHABET_CNT];
            char[] chCnt2 = new char[ALPHABET_CNT];
    
    
            for (int i = 0, n = s1.length(); i < n; i++) {
                chCnt1[s1.charAt(i) - 97] += 1;
                chCnt2[s2.charAt(i) - 97] += 1;
            }
    
            for (int i = 0; i < ALPHABET_CNT; i++) {
                if (chCnt1[i] != chCnt2[i]) {
                    return false;
                }
            }
    
            return true;
        }
        static int sherlockAndAnagrams(String s) {
            int cnt = 0;
            for (int i = 1, n = s.length(); i < n; i++) {
                List<String> subsetList = new ArrayList<>();
                for (int j = 0; j < n; j++) {
                    if (i + j <= n) {
                        subsetList.add(s.substring(j, i + j));
                    }
                }
    
                for (int k = 0, size = subsetList.size(); k < size; k++) {
                    for (int l = k + 1; l < size; l++) {
                        if (isAnagrams(subsetList.get(k), subsetList.get(l))) {
                            cnt++;
                        }
                    }
                }
            }
    
    
            return cnt;
        }
    
        private static final Scanner scanner = new Scanner(System.in);
    
        public static void main(String[] args) throws IOException {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
    
            int q = scanner.nextInt();
            scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
    
            for (int qItr = 0; qItr < q; qItr++) {
                String s = scanner.nextLine();
    
                int result = sherlockAndAnagrams(s);
    
                bufferedWriter.write(String.valueOf(result));
                bufferedWriter.newLine();
            }
    
            bufferedWriter.close();
    
            scanner.close();
        }
    }
    
  • + 0 comments

    My Solution: - Define an array 'subarrs' to store all words(subarrays) of a given size - Compare alll of them and check if they are anagrams Repeat this step for all possible subarrays sizes

    PYTHON `