Sherlock and Anagrams

Sherlock and Anagrams

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the sherlockAndAnagrams function below.
    static int sherlockAndAnagrams(String s) {
        ArrayList subStrings = new ArrayList();
        for(int i = 1; i <= s.length(); i++){
            if(( i%s.length()) == i){
                subStrings.addAll(generateSubstring(s,i));
            }
        }

        int palindroms=0;
        for (int i = 0; i < subStrings.size(); i++){
            for (int j = i+1; j < subStrings.size(); j++){
                if (isPalindrom(subStrings.get(i),subStrings.get(j)))
                palindroms++;
            }
        }
    return palindroms;
    }
    static boolean isPalindrom(String a, String b){
        if(a.length() != b.length()) return false;
        char [] ac = a.toCharArray();
        char [] bc = b.toCharArray();
        Arrays.sort(ac);
        Arrays.sort(bc);
        for(int i = 0; i < ac.length; i++){
            if(ac[i] != bc[i]) return false;
        }
        return true;
    }
    static List generateSubstring (String a, int b){
        List out = new ArrayList();
        for (int i = 0; i < a.length() - b +1; i++){
                out.add(a.substring(i,i+b));
        }
        return out;
    }
    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();
    }
}

Hash Tables: Ransom Note

Tables: Ransom Note

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the checkMagazine function below.
    static void checkMagazine(String[] magazine, String[] note) {
        HashMap magazineMap = new HashMap();
        for(int i = 0;  i < magazine.length; i++){
            if (magazineMap.get(magazine[i]) == null)
            magazineMap.put(magazine[i],1);
            else magazineMap.put(magazine[i], magazineMap.get(magazine[i])+1);
        }
        for (int i = 0; i < note.length; i++){
            if(magazineMap.get(note[i]) == null || magazineMap.get(note[i]) == 0 ) {
               System.out.println("No"); 
               return;
            }
            else magazineMap.put(note[i], magazineMap.get(note[i])-1);
        }
        System.out.println("Yes");
    }

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        String[] mn = scanner.nextLine().split(" ");

        int m = Integer.parseInt(mn[0]);

        int n = Integer.parseInt(mn[1]);

        String[] magazine = new String[m];

        String[] magazineItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < m; i++) {
            String magazineItem = magazineItems[i];
            magazine[i] = magazineItem;
        }

        String[] note = new String[n];

        String[] noteItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {
            String noteItem = noteItems[i];
            note[i] = noteItem;
        }

        checkMagazine(magazine, note);

        scanner.close();
    }
}

Minimum Swaps 2

Minimum Swaps 2

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the minimumSwaps function below.
    static int minimumSwaps(int[] arr) {
        int swaps = 0;
        for (int i = 0; i < arr.length; ){
            if(arr[i] != (i+1)){
                swap(i,arr[i],arr);
                swaps++;
            } else i++;
        }
        return swaps;
    }
    static void swap(int i, int incorrect, int[] arr){
        int temp = arr[incorrect-1];
        arr[incorrect-1] = incorrect;
        arr[i] = temp;
    }
    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 n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        int[] arr = new int[n];

        String[] arrItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < n; i++) {
            int arrItem = Integer.parseInt(arrItems[i]);
            arr[i] = arrItem;
        }

        int res = minimumSwaps(arr);

        bufferedWriter.write(String.valueOf(res));
        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}

2D Array – DS

2D Array – DS

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the hourglassSum function below.
    static int hourglassSum(int[][] arr) {
        int[] dx = {-1,-1,0,0,1,1};
        int[] dy = {1,-1,1,-1,1,-1};
        int max = Integer.MIN_VALUE;
        for ( int i = 1; i< arr.length - 1; i++){
            for (int j =1; j < arr[i].length - 1; j++){
                int current = arr[i][j];
                for (int k = 0; k < 6; k++){
                current += arr[dy[k] + i][dx[k] + j];
            }
                max = Math.max(max,current);
            }
        }
    return max;
    }

    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[][] arr = new int[6][6];

        for (int i = 0; i < 6; i++) {
            String[] arrRowItems = scanner.nextLine().split(" ");
            scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

            for (int j = 0; j < 6; j++) {
                int arrItem = Integer.parseInt(arrRowItems[j]);
                arr[i][j] = arrItem;
            }
        }

        int result = hourglassSum(arr);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}

Balanced Brackets

Balanced Brackets

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the isBalanced function below.
    static String isBalanced(String s) {
LinkedList in = new LinkedList();
    for (int i = 0; i < s.length(); i++){
        if(s.charAt(i) == '{' ||s.charAt(i) == '(' ||s.charAt(i) == '[' ){
            in.push(s.charAt(i));
        }else if (s.charAt(i) == '}'){
            if(!in.isEmpty() && in.peek() == '{') in.pop();
            else return "NO";
        }else if (!in.isEmpty() && s.charAt(i) == ']'){
            if(in.peek() == '[') in.pop();
            else return "NO";
        }else if (!in.isEmpty() && s.charAt(i) == ')'){
            if(in.peek() == '(') in.pop();
            else return "NO";
        }else return "NO";
    }
if (in.isEmpty()) return "YES";
else return "NO";
    }

    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 t = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int tItr = 0; tItr < t; tItr++) {
            String s = scanner.nextLine();

            String result = isBalanced(s);

            bufferedWriter.write(result);
            bufferedWriter.newLine();
        }

        bufferedWriter.close();

        scanner.close();
    }
}

Repeated String

Repeated String

 

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    static long repeatedString(String s, long n) {
        int aInInputString = 0;
        for (int i = 0; i < s.length(); i++)
        if(s.charAt(i) == 'a') aInInputString++;
        long completeRepeats = n / s.length();

        long rem = n % s.length();
        int aInRemaining = 0;
        for (int i = 0; i < rem; i++)
        if(s.charAt(i) == 'a') aInRemaining++;

        return completeRepeats*aInInputString + aInRemaining;
    }

    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")));

        String s = scanner.nextLine();

        long n = scanner.nextLong();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        long result = repeatedString(s, n);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}

303. Range Sum Query – Immutable

https://leetcode.com/problems/range-sum-query-immutable/

class NumArray {
static int[] nums;
    public NumArray(int[] nums) {
     this.nums = nums;   
    }
    
    public int sumRange(int i, int j) {
        if (i == j)
            return nums[i];
        if (i +1 == j)
            return nums[i] + nums[j];
        
        return nums[i] + sumRange(i+1 , j-1) + nums[j];
        

    }
}

/**
 * Your NumArray object will be instantiated and called as such:
 * NumArray obj = new NumArray(nums);
 * int param_1 = obj.sumRange(i,j);
 */