Apple and Orange[Hackerrank]

Problem: Apple and Orange

Solution:

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
https://www.hackerrank.com/contests/w24/challenges/apple-and-orange
By : Ketan Ramteke
https://www.facebook.com/ketan.ramteke.794
https://github.com/TheKetan2
https://www.hackerrank.com/ketantoo
*/

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

public class Solution {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int s = in .nextInt();
		int t = in .nextInt();
		int a = in .nextInt();
		int b = in .nextInt();
		int m = in .nextInt();
		int n = in .nextInt();
		int[] apple = new int[m];
		int manzana = 0, naranja = 0;
		for (int apple_i = 0; apple_i < m; apple_i++) {  
			apple[apple_i] = in .nextInt();  
			int pos = (apple[apple_i] + a);  
			if (pos >= s && pos <= t) 
				manzana++;

		}
		int[] orange = new int[n];
		for (int orange_i = 0; orange_i < n; orange_i++) {  
			orange[orange_i] = in .nextInt();  
			int pos = (orange[orange_i] + b);  
			if (pos >= s && pos <= t) 
				naranja++;
		}
		System.out.println(manzana + "\n" + naranja);
	}
}

Happy Ladybugs

Problem link: Happy Ladybugs

Solution:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
https://www.hackerrank.com/contests/w24/challenges/happy-ladybugs
By: Ketan Ramteke
11 oct 2016
https://www.facebook.com/ketan.ramteke.794
https://github.com/TheKetan2
https://www.hackerrank.com/ketantoo
*/

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

class Solution {
	public static void result(String s) {
		String str = s;
		if (str.length() == 0 || str.length() == 1) {
			System.out.println("YES");
			return;
		}
		if (str.length() == 3 && count(str, '_') == 0) {
			if (str.charAt(0) == str.charAt(1) && str.charAt(1) == str.charAt(2)) {
				System.out.println("YES");
				return;
			} else {
				System.out.println("NO");
				return;
			}

		}

		if (count(str, '_') == str.length()) {
			System.out.println("YES");
			return;
		}

		if (count(str, '_') == 0) {
			if (count(str, str.charAt(0)) == str.length()) {
				System.out.println("YES");
				return;

			}
			int i;

			for (i = 1; i < str.length() - 1; i++) {  
				if (str.charAt(i) != str.charAt(i - 1) && str.charAt(i) != str.charAt(i + 1)) {  
					System.out.println("NO"); return;  
				}  
			}  
			if (str.charAt(0) == str.charAt(1) && str.charAt(str.length() - 2) == str.charAt(str.length() - 1)) {  
				System.out.println("YES");  return;  
			} 
			else {  
				System.out.println("NO");  
				return;  
			}  
		} 
		if (count(str, '_') > 0) {
			str = str.replaceAll("_", "");
			if (str.length() == 1) {
				System.out.println("NO");
				return;
			}

			if (str.length() == 3) {
				if (str.charAt(0) == str.charAt(1) && str.charAt(1) == str.charAt(2)) {
					System.out.println("YES");
					return;
				} else {
					System.out.println("NO");
					return;
				}

			}
			if (count(str, str.charAt(0)) == str.length()) {
				System.out.println("YES");
				return;
			}
			while (str.length() != 0) {
				if (count(str, str.charAt(0)) > 1) {
					String khaliPili = "" + str.charAt(0);
					str = str.replaceAll(khaliPili, "");
				} else break;

			}
			if (str.length() == 0) {
				System.out.println("YES");
				return;
			} else System.out.println("NO");
		}

	}
	public static int count(String s, char ch) {
		String s2 = "" + ch, str = s;
		return str.length() - str.replaceAll(s2, "").length();

	}


	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int Q = in .nextInt();
		for (int a0 = 0; a0 < Q; a0++) {
			int n = in .nextInt();
			String b = in .next();
 //System.out.println(a0+" ");
			result(b);
		}
	}

}