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);
 */

844. Backspace String Compare

844. Backspace String Compare

class Solution {
   public boolean backspaceCompare(String S, String T) {
		LinkedList s = new LinkedList();
		LinkedList t = new LinkedList();

		for (int i = 0; i < S.length(); i++) {
			if (S.charAt(i) == '#') {
				if (!s.isEmpty())
					s.pop();
			} else
				s.push(S.charAt(i));
		}

		for (int i = 0; i < T.length(); i++) {
			if (T.charAt(i) == '#') {
				if (!t.isEmpty())
					t.pop();
			} else
				t.push(T.charAt(i));
		}
		if (s.size() != t.size())
			return false;
		while (!s.isEmpty()) {
			if (s.pop() != t.pop())
				return false;
		}

		return true;
	}
}

929. Unique Email Addresses

929. Unique Email Addresses

class Solution {
    public int numUniqueEmails(String[] emails) {
        		HashSet emailList = new HashSet();
		for (int i = 0; i < emails.length; i++) {
			String[] sp = emails[i].split("@");
			String address = sp[0];
			String domain = sp[1];
			StringBuilder processed_address = new StringBuilder();
			for (int j = 0; j < address.length(); j++) {
				if (address.charAt(j) != '+')
					break;
				else if (address.charAt(j) != '.')
					processed_address.append(address.charAt(j));
			}
			emailList.add(processed_address + "@" + domain);
		}

		return emailList.size();
    }
}