Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();//read rest of line. finish current line's reading
String[] ss = new String[n];
for(int i=0;i
s[i] = in.next();
Quick tips or notes that probably reflects 20 percent of knowledge that usually does 80 percent of job.
6
aaabbb
ab
abc
mnop
xyyx
xaxbbbxx
Sample Output3
1
-1
2
0
1
Explanation 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 | public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int i=0;i<n;i++){ System.out.println(solve(in.next())); } } private static int solve(String s){ if(s==null||s.length()%2==1)return -1; int[] cStatus = new int[26]; for(int i=0;i<s.length()/2;i++){ cStatus[s.charAt(i)-'a']++; } for(int i=s.length()/2;i<s.length();i++){ if(cStatus[s.charAt(i)-'a']>0) cStatus[s.charAt(i)-'a']--; } int sum = 0; for(int i:cStatus) sum+=i; return sum; } } |
5
AAAA
BBBBB
ABABABAB
BABABA
AAABBB
3
4
0
0
4
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 | 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) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner in = new Scanner(System.in); int n = in.nextInt(); for(int i=0;i<n;i++){ numDeletion(in.next()); } } private static void numDeletion(String s){ if(s==null) System.out.println(0); if(s.length()==1)System.out.println(0); int totalDeletion = 0; int i=0,j=1; while(j<s.length()){ while(j<s.length()&&s.charAt(j)==s.charAt(i)) j++; totalDeletion+=j-i-1; i=j; } System.out.println(totalDeletion); } } |
We promptly judged antique ivory buckles for the next prize
Input #2We promptly judged antique ivory buckles for the prize
pangram
Output #2not pangram
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 | 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) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ int bitCheck=0; Scanner in = new Scanner(System.in); StringBuilder sb = new StringBuilder(); while(in.hasNext()){ sb.append(in.next()); } for(int i=0;i<sb.length();i++){ if(Character.isLetter(sb.charAt(i) )){ int n = Character.toLowerCase(sb.charAt(i))-'a'; bitCheck|=1<<n;} } if(bitCheck==0b00000011111111111111111111111111) System.out.println("pangram"); else System.out.println("not pangram"); } } |
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 | int findKthSmallestElement(int a[], int b[], int k) { if (a.length > b.length) return findKthSmallestElement(b, a, k);// this is for next condition // check. /* * Now case when size of smaller array is 0 i.e there is no element in * one array */ if (a.length == 0 && b.length > 0) return b[k - 1]; // due to zero based index /* case where K ==1 that means we have hit limit */ if (k == 1) return Math.min(a[0], b[0]); /* Now the divide and conquer part */ //i and j means length and not index int i = Math.min(k / 2,a.length);// Math.min(sizeA, k/2) ; int j = Math.min(k -i,b.length);// Math.min(sizeB, k/2) ; int[] aTmp, bTmp; if (a[i - 1] > b[j - 1]) { // let's use non-smart way the first aTmp = new int[i]; System.arraycopy(a, 0, aTmp, 0, i); bTmp = new int[b.length - j]; System.arraycopy(b, j, bTmp, 0, b.length - j); // Now we need to find only K-j th element since we have found out // the lowest j return findKthSmallestElement(aTmp, bTmp, k - j); } else { aTmp = new int[a.length - i]; System.arraycopy(a, i, aTmp, 0, a.length - i); bTmp = new int[j]; System.arraycopy(b, 0, bTmp, 0, j); // Now we need to find only K-i th element since we have found out // the lowest i return findKthSmallestElement(aTmp, bTmp, k - i); } } |
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 | static int findKthSmallestElementInPlace(int a[], int b[], int aStart,int aEnd,int bStart, int bEnd,int k) { /* * to maintain uniformity, we will assume that size_a is smaller than * size_b else we will swap array in call :) */ int aLength =aEnd-aStart+1; int bLength = bEnd-bStart+1; if ( aLength > bLength) return findKthSmallestElementInPlace(b, a, bStart,bEnd,aStart,aEnd, k);// this is for next condition // check. /* * Now case when size of smaller array is 0 i.e there is no element in * one array */ if ((aLength == 0 )&& bLength > 0) return b[bStart+k - 1]; // due to zero based index /* case where K ==1 that means we have hit limit */ if (k == 1) return Math.min(a[aStart], b[bStart]); /* Now the divide and conquer part */ int i = Math.min(k/2,a.length);// represents how many steps to advance based on start int j = k -i;// Math.min(sizeB, k/2) ; if (a[aStart+i-1 ] > b[bStart+j-1 ]) { // Now we need to find only K-j th element since we have found out // the lowest j int aS = aStart,aE = aStart+i-1;// including the bigger element int bS = bStart+j,bE = bEnd;//not including smaller element return findKthSmallestElementInPlace(a, b, aS, aE, bS, bE, k-j); } else { // Now we need to find only K-i th element since we have found out // the lowest i int aS = aStart+i,aE = aEnd; int bS = bStart,bE = bStart+j-1; return findKthSmallestElementInPlace(a, b, aS, aE, bS, bE, k-i); } } |
1 2 3 4 5 6 7 8 | static double findMedian(int a[],int b[]){ int len = a.length+b.length; if(len%2==0){ return (double) ((findKthSmallestElement(a, b, len/2)+findKthSmallestElement(a, b, len/2+1))/2.0); } else return (double) findKthSmallestElement(a, b, len/2+1); } |