-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A few Hackerrank Java solutions
- Loading branch information
0 parents
commit 9a22795
Showing
38 changed files
with
1,789 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//https://www.hackerrank.com/challenges/alternating-characters?h_r=next-challenge&h_v=zen | ||
|
||
import java.util.*; | ||
|
||
public class AlternatingCharacter | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
Scanner in = new Scanner(System.in); | ||
int T = in.nextInt(); | ||
|
||
String[] s = new String[T]; | ||
|
||
for( int i = 0 ; i < T ; i++ ) | ||
{ | ||
s[i] = in.next(); | ||
} | ||
|
||
for(int i = 0 ; i < s.length ; i++ ) | ||
{ | ||
int count=0; | ||
char[] c = s[i].toCharArray(); | ||
|
||
for(int j = 0 ; j < c.length - 1 ; j++ ) | ||
{ | ||
if( c[j] == c[j+1] ) | ||
count++; | ||
} | ||
|
||
System.out.println(count); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
public class Anagram | ||
{ | ||
private static final byte ALPHABET_LENGTH = 26; | ||
private static final byte ASCII_ALPHABET_OFFSET = 97; | ||
public static void main(String[] args) throws IOException | ||
{ | ||
StringBuffer sb = new StringBuffer(); | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
for(byte T = Byte.parseByte(br.readLine()); T > 0; --T) | ||
{ | ||
short numChanges = -1; | ||
char[] ab = br.readLine().toCharArray(); | ||
short max = (short)ab.length; | ||
if((max & 1) == 0) | ||
{ | ||
numChanges = 0; | ||
short mid = (short)(max >> 1); | ||
short[] map = new short[ALPHABET_LENGTH]; | ||
for(short i = mid; i < max; map[ab[i++] - ASCII_ALPHABET_OFFSET]++){} | ||
for(short i = 0; i < mid; numChanges += (map[ab[i++] - ASCII_ALPHABET_OFFSET]-- > 0) ? 0 : 1){} | ||
} | ||
sb.append(numChanges + "\n"); | ||
} | ||
System.out.print(sb); | ||
} | ||
} | ||
|
||
/* | ||
public class Anagram | ||
{ | ||
static void isAnagram(String s1, String s2) | ||
{ | ||
//Removing all white spaces from s1 and s2 | ||
String copyOfs1 = s1.replaceAll("\\s", ""); | ||
String copyOfs2 = s2.replaceAll("\\s", ""); | ||
//Initially setting status as true | ||
boolean status = true; | ||
if(copyOfs1.length() != copyOfs2.length()) | ||
{ | ||
//Setting status as false if copyOfs1 and copyOfs2 doesn't have same length | ||
status = false; | ||
} | ||
else | ||
{ | ||
//Changing the case of characters of both copyOfs1 and copyOfs2 and converting them to char array | ||
char[] s1Array = copyOfs1.toLowerCase().toCharArray(); | ||
char[] s2Array = copyOfs2.toLowerCase().toCharArray(); | ||
//Sorting both s1Array and s2Array | ||
Arrays.sort(s1Array); | ||
Arrays.sort(s2Array); | ||
//Checking whether s1Array and s2Array are equal | ||
status = Arrays.equals(s1Array, s2Array); | ||
} | ||
//Output | ||
if(status) | ||
{ | ||
System.out.println(s1+" and "+s2+" are anagrams"); | ||
} | ||
else | ||
{ | ||
System.out.println(s1+" and "+s2+" are not anagrams"); | ||
} | ||
} | ||
public static void main(String[] args) | ||
{ | ||
isAnagram("Mother In Law", "Hitler Woman"); | ||
isAnagram("keEp", "peeK"); | ||
isAnagram("SiLeNt CAT", "LisTen AcT"); | ||
isAnagram("Debit Card", "Bad Credit"); | ||
isAnagram("School MASTER", "The ClassROOM"); | ||
isAnagram("DORMITORY", "Dirty Room"); | ||
isAnagram("ASTRONOMERS", "NO MORE STARS"); | ||
isAnagram("Toss", "Shot"); | ||
isAnagram("joy", "enjoy"); | ||
} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//Given an array, A, of N integers, print each element in reverse order | ||
//as a single line of space-separated integers. | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
import java.text.*; | ||
import java.math.*; | ||
import java.util.regex.*; | ||
|
||
public class ArrRev | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
System.out.println("Input Arr Size : "); | ||
Scanner in = new Scanner(System.in); | ||
int n = in.nextInt(); | ||
int arr[] = new int[n]; | ||
for(int arr_i=0; arr_i < n; arr_i++) | ||
{ | ||
arr[arr_i] = in.nextInt(); | ||
} | ||
System.out.println("REV array is : "); | ||
|
||
for(int i=n;i>0;i--) | ||
{ | ||
System.out.print (arr[i-1]+ " "); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import java.util.Scanner; | ||
|
||
public class BeautifulString | ||
{ | ||
public static void main(String args[]) | ||
{ | ||
Scanner in = new Scanner(System.in); | ||
int n = in.nextInt(); | ||
|
||
String s = in.next(); | ||
|
||
char[] chars = s.toCharArray(); | ||
|
||
int steps = 0; | ||
|
||
for( int i=0 ; i< n-2 ; i++ ) | ||
{ | ||
if (chars[i] == '0' && chars[i + 1] == '1' && chars[i + 2] == '0') | ||
{ | ||
steps++; | ||
chars[i+2] = '1'; | ||
} | ||
} | ||
System.out.println(steps); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//https://www.hackerrank.com/challenges/play-game?h_r=next-challenge&h_v=zen | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.StringTokenizer; | ||
|
||
public class BricksGame | ||
{ | ||
|
||
public static void main(String[] args) throws IOException | ||
{ | ||
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); | ||
String s; | ||
StringTokenizer st; | ||
int T=Integer.parseInt(br.readLine().trim()); | ||
while(T-->0) | ||
{ | ||
int N=Integer.parseInt(br.readLine().trim()); | ||
s=br.readLine().trim(); | ||
st=new StringTokenizer(s); | ||
int arr[]=new int[N]; | ||
long totalscore[]=new long[N]; | ||
for(int i=0;i<N;i++) | ||
{ | ||
arr[i]=Integer.parseInt(st.nextToken()); | ||
|
||
} | ||
totalscore[N-1]=arr[N-1]; | ||
for(int i=N-2;i>=0;i--) | ||
{ | ||
totalscore[i]=totalscore[i+1]+arr[i]; | ||
} | ||
long dp[]=new long[N]; | ||
dp[N-1]=totalscore[N-1]; | ||
dp[N-2]=totalscore[N-2]; | ||
dp[N-3]=totalscore[N-3]; | ||
for(int i=N-4;i>=0;i--) | ||
{ | ||
dp[i]=totalscore[i]-Math.min(dp[i+1],Math.min(dp[i+2],dp[i+3])); | ||
} | ||
System.out.println(dp[0]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import java.util.Scanner; | ||
|
||
public class Candies | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
Scanner sc=new Scanner(System.in); | ||
int N=sc.nextInt(); | ||
sc.nextLine(); | ||
int arr[]=new int[N]; | ||
int i=0; | ||
while(i<N) | ||
{ | ||
arr[i]=sc.nextInt(); | ||
i++; | ||
} | ||
int candie[]=new int[N]; | ||
candie[0]=1; | ||
for( i=1;i<N;i++) | ||
{ | ||
candie[i]=1; | ||
if(arr[i]>arr[i-1]) | ||
{ | ||
candie[i]=candie[i-1]+1; | ||
} | ||
} | ||
int sum=candie[N-1]; | ||
for( i=N-2;i>=0;i--) | ||
{ | ||
if(arr[i]>arr[i+1]) | ||
{ | ||
candie[i]=Math.max(candie[i],candie[i+1]+1); | ||
} | ||
sum+=candie[i]; | ||
} | ||
System.out.println(sum); | ||
sc.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import java.util.*; | ||
|
||
public class CoinChangeProblem | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
Scanner sc=new Scanner(System.in); | ||
int N=sc.nextInt(); | ||
int M=sc.nextInt(); | ||
int i=0; | ||
int coin[]=new int[M]; | ||
sc.nextLine(); | ||
while(i<M) | ||
{ | ||
coin[i]=sc.nextInt(); | ||
i++; | ||
} | ||
long ways[][]=new long[N+1][M+1]; | ||
for(i=0;i<=M;i++) | ||
{ | ||
ways[0][i]=1; | ||
} | ||
for(i=1;i<=N;i++) | ||
{ | ||
for(int j=1;j<=M;j++) | ||
{ | ||
if(coin[j-1]<=i) | ||
{ | ||
ways[i][j]+=ways[i-coin[j-1]][j]; | ||
} | ||
if(j>1) | ||
{ | ||
ways[i][j]+=ways[i][j-1]; | ||
} | ||
} | ||
} | ||
System.out.println(ways[N][M]); | ||
sc.close(); | ||
} | ||
} |
Oops, something went wrong.