Skip to content

Commit b26cdc3

Browse files
committed
Java Programs added
1 parent 61d3432 commit b26cdc3

7 files changed

Lines changed: 411 additions & 0 deletions

Anagram.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
import java.util.Scanner;
3+
/*This java program is used to find whether the strings are is anagram(An anagram is a word or phrase
4+
* formed by rearranging the letters of a different word or phrase, typically
5+
* using all the original letters exactly once) of each other or not or not.*/
6+
public class Anagram {
7+
8+
public static void main(String[] args) {
9+
// TODO Auto-generated method stub
10+
Scanner sc=new Scanner(System.in);
11+
String a=sc.nextLine();
12+
String b=sc.nextLine();
13+
14+
if(a.length()!=b.length())
15+
{
16+
System.out.println("Not Anagram");
17+
}
18+
else
19+
{
20+
boolean flag=true;
21+
int arr[]=new int[256];
22+
/*Storing Number of characters of string a in array according to ASCII values*/
23+
for(char e: a.toCharArray())
24+
{
25+
arr[(int)e]++;
26+
}
27+
/*Storing Number of characters of string b in array according to ASCII values*/
28+
for(char e: b.toCharArray())
29+
{
30+
arr[(int)e]--;
31+
}
32+
/*Comparing the arrays to find whether the characters are same or not in both strings*/
33+
for(int i=0;i<256;i++)
34+
{
35+
if(arr[i]!=0)
36+
{
37+
flag=false;
38+
}
39+
}
40+
if(!flag)
41+
{
42+
System.out.println("Not Anagram");
43+
}
44+
else
45+
System.out.println("Anagram");
46+
}
47+
sc.close();
48+
}
49+
50+
}

MatrixMultiplication.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
import java.util.Scanner;
3+
//Other imports go here, Do NOT change the class name
4+
class MatrixMultiplication
5+
{
6+
public static void main(String[] args)
7+
{
8+
Scanner sc=new Scanner(System.in);
9+
int x=sc.nextInt();
10+
while(x>0)
11+
{
12+
int r1,c1,r2,c2,i,j;
13+
r1=sc.nextInt();
14+
c1=sc.nextInt();
15+
int arr[][]=new int[r1][c1];
16+
17+
for(i=0;i<r1;i++)
18+
{
19+
for(j=0;j<c1;j++)
20+
{
21+
arr[i][j]=sc.nextInt();
22+
}
23+
}
24+
r2=sc.nextInt();
25+
c2=sc.nextInt();
26+
int b[][]=new int[r2][c2];
27+
int res[][]=new int[r1][c2];
28+
for(i=0;i<r2;i++)
29+
{
30+
for(j=0;j<c2;j++)
31+
{
32+
b[i][j]=sc.nextInt();
33+
}
34+
}
35+
36+
for(i=0;i<r1;i++)
37+
{
38+
for(j=0;j<c2;j++)
39+
{
40+
int sum=0;
41+
for(int k=0;k<r2;k++)
42+
{
43+
sum+=arr[i][k]*b[k][j];
44+
}
45+
res[i][j]=sum;
46+
}
47+
}
48+
for(int r=0;r<r1;r++)
49+
{
50+
for(int y=0;y<c2;y++)
51+
System.out.print(res[r][y]+" ");
52+
System.out.println();
53+
}
54+
x--;
55+
}
56+
sc.close();
57+
}
58+
}

PangramCheck.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
3+
/*Given a string check if it is Pangram or not. A pangram is a sentence containing every letter in the English Alphabet.
4+
5+
Example 1:
6+
7+
Input:
8+
S = Bawds jog, flick quartz, vex nymph
9+
Output: 1
10+
Explantion: In the given input, there
11+
are all the letters of the English
12+
alphabet. Hence, the output is 1.
13+
Example 2:
14+
15+
Input:
16+
S = sdfs
17+
Output: 0
18+
Explantion: In the given input, there
19+
aren't all the letters present in the
20+
English alphabet. Hence, the output
21+
is 0.
22+
Your Task:
23+
You need to complete the function checkPangram() that takes a string as a parameter and returns true if the string is a pangram, else it returns false.
24+
25+
Expected Time Complexity: O(N).
26+
Expected Auxiliary Space: O(Number of distinct characters).
27+
28+
Constraints:
29+
1 <= |S| <= 104*/
30+
import java.util.*;
31+
class PangramCheck
32+
{
33+
public static void main (String[] args)
34+
{
35+
Scanner sc=new Scanner(System.in);
36+
String str=sc.next();
37+
int arr[]=new int[26];
38+
for(int i=0;i<str.length();i++)
39+
{
40+
if(str.charAt(i)>='a'&&str.charAt(i)<='z')
41+
{
42+
arr[str.charAt(i)-'a']++;
43+
}
44+
else if(str.charAt(i)>='A'&&str.charAt(i)<='Z')
45+
{
46+
arr[str.charAt(i)-'A']++;
47+
}
48+
}
49+
for(int i=0;i<26;i++)
50+
{
51+
if(arr[i]==0)
52+
{
53+
System.out.println("Not Pangram");
54+
return;
55+
}
56+
}
57+
System.out.println("Pangram");
58+
sc.close();
59+
}
60+
61+
62+
}

ParenthesisChecker.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
2+
/*Given an expression string exp. Examine whether the pairs and the orders of “{“,”}”,”(“,”)”,”[“,”]” are correct in exp.
3+
For example, the program should print 'balanced' for exp = “[()]{}{[()()]()}” and 'not balanced' for exp = “[(])”
4+
5+
Input:
6+
The first line of input contains an integer T denoting the number of test cases. Each test case consists of a string of expression, in a separate line.
7+
8+
Output:
9+
Print 'balanced' without quotes if the pair of parenthesis is balanced else print 'not balanced' in a separate line.
10+
11+
Constraints:
12+
1 ≤ T ≤ 100
13+
1 ≤ |s| ≤ 105
14+
15+
Example:
16+
Input:
17+
3
18+
{([])}
19+
()
20+
([]
21+
22+
Output:
23+
balanced
24+
balanced
25+
not balanced*/
26+
27+
import java.util.*;
28+
import java.io.*;
29+
30+
class ParenthesisChecker {
31+
public static void main (String[] args) {
32+
Scanner sc=new Scanner(System.in);
33+
int n=sc.nextInt();
34+
sc.nextLine();
35+
while(n-->0)
36+
{
37+
boolean flag=true;
38+
String str=sc.nextLine();
39+
Stack<Character> stk=new Stack<>();
40+
for(int i=0;i<str.length();i++)
41+
{
42+
if(str.charAt(i)=='('||str.charAt(i)=='{'||str.charAt(i)=='[')
43+
{
44+
stk.push(str.charAt(i));
45+
}
46+
else if(stk.empty()&&(str.charAt(i)==')'||str.charAt(i)=='}'||str.charAt(i)==']'))
47+
{
48+
flag=false;
49+
break;
50+
}
51+
else if(!stk.empty()&&str.charAt(i)==')')
52+
{
53+
if(stk.peek()=='(')
54+
stk.pop();
55+
else
56+
{
57+
flag=false;
58+
break;
59+
}
60+
}
61+
else if(!stk.empty()&&str.charAt(i)==']')
62+
{
63+
if(stk.peek()=='[')
64+
stk.pop();
65+
else
66+
{
67+
flag=false;
68+
break;
69+
}
70+
}
71+
else if(!stk.empty()&&str.charAt(i)=='}')
72+
{
73+
if(stk.peek()=='{')
74+
stk.pop();
75+
else
76+
{
77+
flag=false;
78+
break;
79+
}
80+
}
81+
}
82+
if(flag==true&&stk.empty())
83+
{
84+
System.out.println("balanced");
85+
}
86+
else
87+
{
88+
System.out.println("not balanced");
89+
}
90+
}
91+
92+
}
93+
}

RockPaperScissor.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
/*The RPS world championship is here. Here two players A and B play the game. You need to determine who wins.
3+
Both players can choose moves from the set {R,P,S}.
4+
The game is a draw if both players choose the same item.
5+
The winning rules of RPS are given below:
6+
7+
Rock crushes scissor
8+
Scissor cuts paper
9+
Paper envelops rock
10+
Input:
11+
The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains single line of input that contains two characters sidebyside. These characters denote the moves of players A and B respectively.
12+
13+
Output:
14+
For each testcase, in a newline, print the winner. If match is draw, print 'DRAW'.
15+
16+
Constraints:
17+
1<= T <= 50
18+
19+
Example:
20+
Input:
21+
7
22+
RR
23+
RS
24+
SR
25+
SP
26+
PP
27+
PS
28+
RP
29+
Output:
30+
DRAW
31+
A
32+
B
33+
A
34+
DRAW
35+
B
36+
B*/
37+
import java.util.*;
38+
39+
class GFG {
40+
public static void main (String[] args) {
41+
Scanner sc=new Scanner(System.in);
42+
int t=sc.nextInt();
43+
sc.nextLine();
44+
while(t!=0)
45+
{
46+
String str=sc.nextLine();
47+
if(str.charAt(0)==str.charAt(1))
48+
{
49+
System.out.println("DRAW");
50+
}
51+
else if(str.equals("RS")||str.equals("PR")||str.equals("SP"))
52+
{
53+
System.out.println("A");
54+
55+
}
56+
else
57+
System.out.println("B");
58+
t--;
59+
}
60+
sc.close();
61+
}
62+
}

0 commit comments

Comments
 (0)