Skip to content

Commit b976f4d

Browse files
added java hacker rank questions
1 parent baf8ed3 commit b976f4d

39 files changed

+1545
-5
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Java program to implement a stack using singly linked
2+
// list
3+
4+
// Class representing a node in the linked list
5+
class Node {
6+
int data;
7+
Node next;
8+
Node(int new_data) {
9+
this.data = new_data;
10+
this.next = null;
11+
}
12+
}
13+
14+
// Class to implement stack using a singly linked list
15+
class Stack {
16+
17+
// Head of the linked list
18+
Node head;
19+
20+
// Constructor to initialize the stack
21+
Stack() { this.head = null; }
22+
23+
// Function to check if the stack is empty
24+
boolean isEmpty() {
25+
26+
// If head is null, the stack is empty
27+
return head == null;
28+
}
29+
30+
// Function to push an element onto the stack
31+
void push(int new_data) {
32+
33+
// Create a new node with given data
34+
Node new_node = new Node(new_data);
35+
36+
// Check if memory allocation for the new node
37+
// failed
38+
if (new_node == null) {
39+
System.out.println("\nStack Overflow");
40+
return;
41+
}
42+
43+
// Link the new node to the current top node
44+
new_node.next = head;
45+
46+
// Update the top to the new node
47+
head = new_node;
48+
}
49+
50+
// Function to remove the top element from the stack
51+
void pop() {
52+
53+
// Check for stack underflow
54+
if (isEmpty()) {
55+
System.out.println("\nStack Underflow");
56+
return;
57+
}
58+
else {
59+
60+
// Assign the current top to a temporary
61+
// variable
62+
Node temp = head;
63+
64+
// Update the top to the next node
65+
head = head.next;
66+
67+
// Deallocate the memory of the old top node
68+
temp = null;
69+
}
70+
}
71+
72+
// Function to return the top element of the stack
73+
int peek() {
74+
75+
// If stack is not empty, return the top element
76+
if (!isEmpty())
77+
return head.data;
78+
else {
79+
System.out.println("\nStack is empty");
80+
return Integer.MIN_VALUE;
81+
}
82+
}
83+
}
84+
85+
// Driver code
86+
public class Stack_linkedlist {
87+
// stack<Character>st = new stack<>();
88+
89+
public static void main(String[] args)
90+
{
91+
// Creating a stack
92+
Stack st = new Stack();
93+
94+
// Push elements onto the stack
95+
st.push(11);
96+
st.push(22);
97+
st.push(33);
98+
st.push(44);
99+
100+
// Print top element of the stack
101+
System.out.println("Top element is " + st.peek());
102+
103+
// removing two elemements from the top
104+
System.out.println("Removing two elements...");
105+
st.pop();
106+
st.pop();
107+
108+
// Print top element of the stack
109+
System.out.println("Top element is " + st.peek());
110+
}
111+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.Scanner;
2+
3+
public class Java_10_anagaram {
4+
static boolean isAnagram(String a, String b) {
5+
// Complete the function
6+
a=a.toUpperCase();
7+
b=b.toUpperCase();
8+
int arrA[]= new int[26];
9+
int arrB[]= new int[26];
10+
11+
if(a.length() !=b.length()){
12+
return false;
13+
}
14+
15+
for(int i=0;i<a.length();i++){
16+
arrA[a.charAt(i)-'A']++;
17+
arrB[b.charAt(i)-'A']++;
18+
}
19+
20+
for(int i=0;i<26;i++){
21+
if(arrA[i]!=arrB[i]){
22+
return false;
23+
}
24+
}
25+
return true;
26+
}
27+
28+
public static void main(String[] args) {
29+
30+
Scanner scan = new Scanner(System.in);
31+
String a = scan.next();
32+
String b = scan.next();
33+
scan.close();
34+
boolean ret = isAnagram(a, b);
35+
System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
36+
}
37+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Java_11_string_token {
5+
6+
public static void main(String[] args) {
7+
Scanner scan = new Scanner(System.in);
8+
String s = scan.nextLine();
9+
scan.close();
10+
List<String>ans = new ArrayList<>();
11+
int prev=0;
12+
for(int i=0;i<s.length();i++){
13+
char ch=s.charAt(i);
14+
if((ch>='A' && ch<='Z') ||(ch>='a' && ch<='z')){
15+
continue;
16+
} else{
17+
if(prev != i){
18+
ans.add(s.substring(prev,i));
19+
}
20+
21+
prev=i+1;
22+
}
23+
}
24+
char ch = s.charAt(s.length()-1);
25+
if((ch>='A' && ch<='Z') ||(ch>='a' && ch<='z')){
26+
ans.add(s.substring(prev));
27+
}
28+
29+
System.out.println(ans.size());
30+
for(String a: ans){
31+
System.out.println(a);
32+
}
33+
34+
//same above solution
35+
String words[]=s.trim().split("[ !,?.\\_'@]+");
36+
System.out.println(words.length);
37+
for (String word:words)
38+
System.out.println(word);
39+
}
40+
}
41+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.Scanner;
2+
import java.util.regex.*;
3+
4+
public class Java_12_pattern {
5+
public static void main(String args[]){
6+
Scanner sc = new Scanner(System.in);
7+
int n= sc.nextInt();
8+
//just to clean buffer
9+
if (sc.hasNextLine()) {
10+
sc.nextLine();
11+
}
12+
while(n>0){
13+
String s = sc.nextLine();
14+
try{
15+
Pattern.compile(s);
16+
System.out.println("Valid");
17+
}catch (PatternSyntaxException e){
18+
System.out.println("Invalid");
19+
}
20+
n=n-1;
21+
}
22+
sc.close();
23+
}
24+
public static void main1(){
25+
Scanner in = new Scanner(System.in);
26+
//everything should be in in.nextLine();
27+
int testCases = Integer.parseInt(in.nextLine());
28+
while(testCases > 0){
29+
String pattern = in.nextLine();
30+
try {
31+
Pattern.compile(pattern);
32+
System.out.println("Valid");
33+
} catch (PatternSyntaxException e) {
34+
System.out.println("Invalid");
35+
}
36+
testCases--;
37+
}
38+
}
39+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.Scanner;
2+
import java.util.regex.Matcher;
3+
import java.util.regex.Pattern;
4+
5+
class MyRegex {
6+
// 1--> it can contain single digit i.e ([0-9]);
7+
//
8+
//2--> It can contain two digits i.e ([0-9][0-9]);
9+
//
10+
//3--> Range is (099 to 199)i.e((0|1)[0-9][0-9]);
11+
//–
12+
//4--> range is (200 - 249) i.e (2[0-4][0-9]) ;
13+
//
14+
//5--> range is (250-255) i.e (25[0-5]);
15+
//
16+
// Total :"([0-9]|[0-9][0-9]|(0|1)[0-9][0-9]|2[0-4][0-9]|25[0-5])"
17+
String zerotoMax = "([0-9]|[0-9][0-9]|(0|1)[0-9][0-9]|2[0-4][0-9]|25[0-5])";
18+
String pattern = zerotoMax+"."+zerotoMax+"."+zerotoMax+"."+zerotoMax;
19+
}
20+
21+
public class Java_13_ip_regex {
22+
23+
public static void main(String[] args) {
24+
Scanner sc = new Scanner(System.in);
25+
while(sc.hasNext())
26+
{
27+
String IP = sc.next();
28+
System.out.println(IP.matches(new MyRegex().pattern));
29+
}
30+
31+
}
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.math.BigInteger;
2+
import java.util.Scanner;
3+
4+
public class Java_14_BigNumber {
5+
public static void main(String args[]){
6+
Scanner sc = new Scanner(System.in);
7+
String s = sc.nextLine();
8+
String n1= sc.nextLine();
9+
String n2 = sc.nextLine();
10+
sc.close();
11+
12+
BigInteger b = new BigInteger(s);
13+
if(b.isProbablePrime(10)){
14+
System.out.println("Prime");
15+
}else{
16+
System.out.println("not prime");
17+
}
18+
19+
BigInteger nb1 = new BigInteger(n1);
20+
BigInteger nb2 = new BigInteger(n2);
21+
BigInteger sum = nb1.add(nb2);
22+
BigInteger mul = nb1.multiply(nb2);
23+
24+
System.out.println(sum);
25+
System.out.println(mul);
26+
27+
}
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Animal{
2+
void walk(){
3+
System.out.println("I am walking");
4+
}
5+
}
6+
7+
class Bird extends Animal {
8+
void fly(){
9+
System.out.println("I am flying");
10+
}
11+
}
12+
13+
public class Java_15_inheritance {
14+
public static void main(String argd[]){
15+
Bird b = new Bird();
16+
b.fly();
17+
b.walk();
18+
//print super class name
19+
System.out.println(b.getClass().getSuperclass().getName());
20+
21+
Animal a = new Animal();
22+
a.walk();
23+
24+
Animal a2 = new Bird(); //only walk will allow (generalization)
25+
a2.walk();
26+
// a2.fly();
27+
28+
if(a instanceof Bird){
29+
System.out.println("possible specialization");
30+
Bird b3 = (Bird) a;
31+
}else{
32+
System.out.println("not possible specialisation");
33+
}
34+
// Bird b2 = (Bird) new Animal(); //need to type caste (specialization)
35+
// b2.walk();
36+
// b2.fly();
37+
}
38+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Arithmetic {
2+
int add(int x, int y){
3+
int z =x+y;
4+
return z;
5+
}
6+
}
7+
8+
class Adder extends Arithmetic{
9+
10+
}
11+
12+
public class Java_16_inheritance {
13+
public static void main(String args[]){
14+
Adder a = new Adder();
15+
int result = a.add(10,20);
16+
System.out.println(result);
17+
}
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.Scanner;
2+
3+
abstract class Book{
4+
String title;
5+
abstract void setTitle(String s);
6+
String getTitle(){
7+
return title;
8+
}
9+
}
10+
11+
class MyBook extends Book {
12+
@Override
13+
void setTitle(String s){
14+
this.title = s;
15+
}
16+
}
17+
18+
public class Java_17_AbstractClass {
19+
public static void main(String args[]){
20+
Scanner sc = new Scanner(System.in);
21+
String title=sc.nextLine();
22+
MyBook myBook = new MyBook();
23+
myBook.setTitle(title);
24+
System.out.println("The title is: "+myBook.getTitle());
25+
sc.close();
26+
}
27+
28+
}

0 commit comments

Comments
 (0)