├── .classpath
├── .project
├── .settings
├── org.eclipse.core.resources.prefs
└── org.eclipse.jdt.core.prefs
├── README.md
├── bin
├── Pattern
│ ├── ArmstrongNumber.class
│ ├── BetterSunglassesPattern.class
│ ├── FibonacciWithRecursion.class
│ ├── FibonacciWithoutRecursion.class
│ ├── HomePattern.class
│ ├── IndianFlag.class
│ ├── IndianFlagUsingLoops.class
│ ├── Left_Triangle_Star_Pattern.class
│ ├── Pattern1.class
│ ├── Pyramid.class
│ ├── StarPrint.class
│ ├── StarfishPattern.class
│ ├── leftangle.class
│ ├── m.class
│ ├── pattern2.class
│ ├── pattern3.class
│ └── prime.class
├── String
│ ├── CountCharacterOccurrences.class
│ ├── CountVowelsAndConsonants.class
│ ├── DuplicateCharactersFinder.class
│ ├── FirstNonRepeatedCharacter.class
│ ├── NameToBinary.class
│ ├── StringPermutations.class
│ ├── StringPractice1.class
│ ├── StringPractice2.class
│ ├── StringReverseRecursion.class
│ └── stringprectice4.class
└── module-info.class
└── src
├── Pattern
├── ArmstrongNumber.java
├── BetterSunglassesPattern.java
├── FibonacciWithRecursion.java
├── FibonacciWithoutRecursion.java
├── HomePattern.java
├── IndianFlag.java
├── IndianFlagUsingLoops.java
├── Left_Triangle_Star_Pattern.java
├── Pattern1.java
├── Pyramid.java
├── StarPrint.java
├── StarfishPattern.java
├── leftangle.java
├── m.java
├── pattern2.java
├── pattern3.java
└── prime.java
├── String
├── CountCharacterOccurrences.java
├── CountVowelsAndConsonants.java
├── DuplicateCharactersFinder.java
├── FirstNonRepeatedCharacter.java
├── NameToBinary.java
├── StringPermutations.java
├── StringPractice1.java
├── StringPractice2.java
├── StringReverseRecursion.java
└── stringprectice4.java
└── module-info.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | Data_Structure_and_Algorithm_usingJava
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.core.resources.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | encoding/=UTF-8
3 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=17
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
11 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12 | org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
13 | org.eclipse.jdt.core.compiler.release=enabled
14 | org.eclipse.jdt.core.compiler.source=17
15 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DataStructureandAlgorithmjava
2 | Top 100 Data Structure and Algorithm Interview Questions for Java Programmers
3 | Data structure and algorithms are a core part of any Programming job interview. It doesn't matter whether you are a C++ developer, a Java developer, or a Web developer working in JavaScript, Angular, React, or Query. As a computer science graduate, it's expected from a program to have strong knowledge of both basic data structures, like the array, linked list, binary tree, hash table, Stack, Queue, and advanced data structures like the binary heap, trie, self-balanced tree, circular buffer, etc.
4 |
5 | I have taken a lot of Java interviews for both junior and senior positions in the past, and I have also been involved in interviewing C++ developers. One difference that I have clearly noticed between a C++ and a Java developer is their understanding and command of Data structure and algorithms.
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/bin/Pattern/ArmstrongNumber.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/ArmstrongNumber.class
--------------------------------------------------------------------------------
/bin/Pattern/BetterSunglassesPattern.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/BetterSunglassesPattern.class
--------------------------------------------------------------------------------
/bin/Pattern/FibonacciWithRecursion.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/FibonacciWithRecursion.class
--------------------------------------------------------------------------------
/bin/Pattern/FibonacciWithoutRecursion.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/FibonacciWithoutRecursion.class
--------------------------------------------------------------------------------
/bin/Pattern/HomePattern.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/HomePattern.class
--------------------------------------------------------------------------------
/bin/Pattern/IndianFlag.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/IndianFlag.class
--------------------------------------------------------------------------------
/bin/Pattern/IndianFlagUsingLoops.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/IndianFlagUsingLoops.class
--------------------------------------------------------------------------------
/bin/Pattern/Left_Triangle_Star_Pattern.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/Left_Triangle_Star_Pattern.class
--------------------------------------------------------------------------------
/bin/Pattern/Pattern1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/Pattern1.class
--------------------------------------------------------------------------------
/bin/Pattern/Pyramid.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/Pyramid.class
--------------------------------------------------------------------------------
/bin/Pattern/StarPrint.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/StarPrint.class
--------------------------------------------------------------------------------
/bin/Pattern/StarfishPattern.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/StarfishPattern.class
--------------------------------------------------------------------------------
/bin/Pattern/leftangle.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/leftangle.class
--------------------------------------------------------------------------------
/bin/Pattern/m.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/m.class
--------------------------------------------------------------------------------
/bin/Pattern/pattern2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/pattern2.class
--------------------------------------------------------------------------------
/bin/Pattern/pattern3.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/pattern3.class
--------------------------------------------------------------------------------
/bin/Pattern/prime.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/Pattern/prime.class
--------------------------------------------------------------------------------
/bin/String/CountCharacterOccurrences.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/String/CountCharacterOccurrences.class
--------------------------------------------------------------------------------
/bin/String/CountVowelsAndConsonants.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/String/CountVowelsAndConsonants.class
--------------------------------------------------------------------------------
/bin/String/DuplicateCharactersFinder.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/String/DuplicateCharactersFinder.class
--------------------------------------------------------------------------------
/bin/String/FirstNonRepeatedCharacter.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/String/FirstNonRepeatedCharacter.class
--------------------------------------------------------------------------------
/bin/String/NameToBinary.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/String/NameToBinary.class
--------------------------------------------------------------------------------
/bin/String/StringPermutations.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/String/StringPermutations.class
--------------------------------------------------------------------------------
/bin/String/StringPractice1.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/String/StringPractice1.class
--------------------------------------------------------------------------------
/bin/String/StringPractice2.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/String/StringPractice2.class
--------------------------------------------------------------------------------
/bin/String/StringReverseRecursion.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/String/StringReverseRecursion.class
--------------------------------------------------------------------------------
/bin/String/stringprectice4.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/String/stringprectice4.class
--------------------------------------------------------------------------------
/bin/module-info.class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ManishParashar0/DataStructureandAlgorithmjava/2f319d11fa48dbfa4b087250c7fb1d9a48db69d2/bin/module-info.class
--------------------------------------------------------------------------------
/src/Pattern/ArmstrongNumber.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 |
3 | class ArmstrongNumber {
4 | public static void main(String[] args) {
5 | int sumOfCube = 0, a, temp;
6 | int number = 153;
7 | temp = number;
8 | while (number > 0) {
9 | a = number % 10; // Getting last digit
10 | number = number / 10; // Removing last digit from number
11 | sumOfCube = sumOfCube + (a * a * a); // Calculating sum of cubes of digits
12 | }
13 | if (temp == sumOfCube)
14 | System.out.println(temp + " is an armstrong number");
15 | else
16 | System.out.println(temp + "is not an armstrong number");
17 | }
18 | }
--------------------------------------------------------------------------------
/src/Pattern/BetterSunglassesPattern.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 | public class BetterSunglassesPattern {
3 | public static void main(String[] args) {
4 | int height = 7; // Adjust the height of the sunglasses pattern
5 |
6 | drawSunglasses(height);
7 | }
8 |
9 | public static void drawSunglasses(int height) {
10 | int width = height * 6;
11 | int lensWidth = width / 2 - 1;
12 |
13 | for (int row = 1; row <= height; row++) {
14 | for (int col = 1; col <= width; col++) {
15 | if (col <= row || col > width - row + 1) {
16 | System.out.print("/");
17 | } else if (col >= width / 2 + 1 && col <= width / 2 + lensWidth) {
18 | System.out.print("=");
19 | } else if (col == width / 2 + lensWidth + 1 || col == width / 2 + lensWidth + 2) {
20 | System.out.print(".");
21 | } else {
22 | System.out.print(" ");
23 | }
24 | }
25 | System.out.println();
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/Pattern/FibonacciWithRecursion.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 |
3 | class FibonacciWithRecursion {
4 | static int number1 = 0, number2 = 1, number3 = 0;
5 |
6 | static void Fibo(int count) {
7 | if (count > 0) {
8 | number3 = number1 + number2;
9 | number1 = number2;
10 | number2 = number3;
11 | System.out.print(" " + number3);
12 | Fibo(count - 1);
13 | }
14 | }
15 |
16 | public static void main(String args[]) {
17 | int count = 5;
18 | // print 0 and 1
19 | System.out.print(number1 + " " + number2);
20 | // 2 numbers are already printed so using recursion we print the remaining n-2
21 | // numbers
22 | Fibo(count - 2);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Pattern/FibonacciWithoutRecursion.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 |
3 | class FibonacciWithoutRecursion {
4 | public static void main(String args[]) {
5 | int number1 = 0, number2 = 1, number3, i, count = 5;
6 | // printing 0 and 1
7 | System.out.print(number1 + " " + number2);
8 |
9 | // 0 and 1 is already printed so loop will start with 2
10 | for (i = 2; i < count; ++i) {
11 | number3 = number1 + number2;
12 | System.out.print(" " + number3);
13 | number1 = number2;
14 | number2 = number3;
15 | }
16 | }
17 | }
--------------------------------------------------------------------------------
/src/Pattern/HomePattern.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 |
3 | public class HomePattern {
4 | public static void main(String[] args) {
5 | int height = 6; // Adjust the height of the home pattern
6 |
7 | drawHome(height);
8 | }
9 |
10 | /*
11 | * interview practice public HomePattern() { // TODO Auto-generated constructor
12 | * stub }
13 | *
14 | * public HomePattern(int a) { // TODO Auto-generated constructor stub }
15 | */
16 | public static void drawHome(int height) {
17 |
18 | // HomePattern homePattern = new HomePattern();
19 | int width = height * 2;
20 |
21 | // Draw roof
22 | for (int row = 1; row <= height; row++) {
23 | for (int col = 1; col <= width; col++) {
24 | if (col > height - row && col < height + row) {
25 | System.out.print("*");
26 | } else {
27 | System.out.print(" ");
28 | }
29 | }
30 | System.out.println();
31 | }
32 |
33 | // Draw walls and door
34 | for (int row = 1; row <= height / 2; row++) {
35 | for (int col = 1; col <= width; col++) {
36 | if ((col == 1 || col == width) || (col >= height / 2 && col <= width - height / 2)) {
37 | System.out.print("|");
38 | } else if (col == width / 2) {
39 | System.out.print("+");
40 | } else {
41 | System.out.print(" ");
42 | }
43 | }
44 | System.out.println();
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/Pattern/IndianFlag.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 |
3 | public class IndianFlag {
4 | // main() method start
5 | public static void main(String[] args) {
6 |
7 | // declare and initialize variables
8 | int height = 26;
9 | int width = 40;
10 | int temp = 3;
11 | // Safwan
12 | // we use only one nested loop to print all the parts of the flag
13 | for (int m = 1; m <= height; m++) {
14 | for (int n = 1; n <= width; n++) {
15 |
16 | // create first layer of Indian flag
17 | if (m <= 4) {
18 | if (m == 1 || m == 4) {
19 | if (n >= 15 && n <= 35) {
20 | System.out.print("-");
21 | } else {
22 | System.out.print(" ");
23 | }
24 | } else {
25 | if (m == 2 || m == 3) {
26 | if (n == 15 || n == 35) {
27 | System.out.print("|");
28 | } else {
29 | System.out.print(" ");
30 | }
31 | }
32 | }
33 | }
34 | if (m > 4 && m <= 8) {
35 | // second layer of the flag
36 | if (m <= 7) {
37 | if (n == 15 || n == 35) {
38 | System.out.print("|");
39 | } else {
40 | if (n >= 22 && n <= 28) {
41 | System.out.print("*");
42 | } else {
43 | System.out.print(" ");
44 | }
45 | }
46 | } else {
47 | if (n >= 15 && n <= 35) {
48 | System.out.print("-");
49 | } else {
50 | System.out.print(" ");
51 | }
52 | }
53 | }
54 | if (m > 8 && m <= 11) {
55 | // third layer of the flag
56 | if (m <= 10) {
57 | if (n == 15 || n == 35) {
58 | System.out.print("|");
59 | } else {
60 | System.out.print(" ");
61 | }
62 | } else {
63 | if (n >= 15 && n <= 35) {
64 | System.out.print("-");
65 | } else {
66 | System.out.print(" ");
67 | }
68 | }
69 | }
70 | if (m >= 12 && m <= 19) {
71 | // stick of the flag
72 | if (n == 15) {
73 | System.out.print("|");
74 | } else {
75 | System.out.print(" ");
76 | }
77 | }
78 | // stairs of the flag
79 | if (m >= 20) {
80 | if (m % 2 == 0) {
81 | if (n >= 15 - temp && n <= 15 + temp) {
82 | System.out.print("-");
83 | } else {
84 | System.out.print(" ");
85 | }
86 | } else {
87 | if (n == 15 - temp || n == 15 + temp) {
88 | System.out.print("|");
89 | } else {
90 | System.out.print(" ");
91 | }
92 | }
93 | }
94 | }
95 | if (m >= 20) {
96 | if (m % 2 != 0) {
97 | temp++;
98 | }
99 | }
100 | System.out.print("\n");
101 | }
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/src/Pattern/IndianFlagUsingLoops.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 |
3 | public class IndianFlagUsingLoops {
4 |
5 | public static void main(String[] args) {
6 | int height = 15; // Adjust the height as needed
7 | int width = 20; // Adjust the width as needed
8 |
9 | // Saffron Stripe
10 | for (int i = 0; i < height / 3; i++) {
11 | for (int j = 0; j < width; j++) {
12 | System.out.print("*");
13 | }
14 | System.out.println();
15 | }
16 |
17 | // White Stripe
18 | for (int i = 0; i < height / 3; i++) {
19 | for (int j = 0; j < width; j++) {
20 | System.out.print(" ");
21 | }
22 | System.out.println();
23 | }
24 |
25 | // Green Stripe
26 | for (int i = 0; i < height / 3; i++) {
27 | for (int j = 0; j < width; j++) {
28 | System.out.print("*");
29 | }
30 | System.out.println();
31 | }
32 |
33 | // Ashoka Chakra
34 | int centerX = width / 2;
35 | int centerY = height / 2;
36 | int chakraRadius = height / 6;
37 |
38 | for (int i = 0; i < height; i++) {
39 | for (int j = 0; j < width; j++) {
40 | if ((Math.pow(j - centerX, 2) + Math.pow(i - centerY, 2)) <= Math.pow(chakraRadius, 2)) {
41 | System.out.print("B"); // Blue color for Ashoka Chakra
42 | } else {
43 | System.out.print(" ");
44 | }
45 | }
46 | System.out.println();
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/Pattern/Left_Triangle_Star_Pattern.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 |
3 | public class Left_Triangle_Star_Pattern {
4 | public static void main(String[] args) {
5 | int i, j;
6 | for (i = 0; i <= 5; i++) {
7 | for (j = 0; j <= i; j++) {
8 | System.out.print(" ");
9 | }
10 |
11 | System.out.println(" *");
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/Pattern/Pattern1.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 |
3 | public class Pattern1 {
4 | public static void main(String[] args) {
5 | for (int i = 0; i < 7; i++) {
6 |
7 | for (int j = 0; j < 7; j++) {
8 | if (i == 5 && j == 5) {
9 | System.out.println("+");
10 | }
11 | if (i == 1 && j == 1) {
12 |
13 | }
14 |
15 | }
16 | }
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/src/Pattern/Pyramid.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 |
3 | public class Pyramid {
4 |
5 | public static void pyramidPattern(int n) {
6 | for (int i = 0; i < n; i++) { // outer loop for number of rows(n)
7 | for (int j = n - i; j > 1; j--) { // inner loop for spaces
8 | System.out.print(" "); // print space
9 | }
10 | for (int j = 0; j <= i; j++) { // inner loop for number of columns
11 | System.out.print("* "); // print star
12 | }
13 |
14 | System.out.println(); // ending line after each row
15 | }
16 | }
17 |
18 | public static void main(String args[]) { // driver function
19 | int n = 5;
20 | pyramidPattern(n);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/Pattern/StarPrint.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 |
3 | public class StarPrint {
4 | public static void main(String[] args) {
5 | int i, j;
6 | for (i = 0; i <= 5; i++) {
7 | for (j = 0; j <= i; j++) {
8 | System.out.print(" *");
9 | }
10 | System.out.println(" ");
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/Pattern/StarfishPattern.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 |
3 | public class StarfishPattern {
4 | public static void main(String[] args) {
5 | int size = 5; // Adjust the size of the starfish pattern
6 |
7 | drawStarfish(size);
8 | }
9 |
10 | public static void drawStarfish(int size) {
11 | int totalRows = size * 2 + 1;
12 |
13 | for (int row = 1; row <= totalRows; row++) {
14 | for (int col = 1; col <= totalRows; col++) {
15 | if (col == row || col == totalRows - row + 1) {
16 | System.out.print("*");
17 | } else if (row == size + 1) {
18 | System.out.print("*");
19 | } else {
20 | System.out.print(" ");
21 | }
22 | }
23 | System.out.println();
24 | }
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/Pattern/leftangle.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 |
3 | public class leftangle {
4 | public static void main(String[] args) {
5 | /*
6 | * for (int i = 0; i < 5; i++) { for (int j = 0; j <= i; j++) {
7 | *
8 | * System.out.print(" *"); } System.out.println(""); }
9 | */
10 |
11 | for (int a = 1; a <= 5; a++) {
12 | for (int b = 1; b <= 5; b++) {
13 | if (a == 5 || b == 1 || b == a) {
14 | System.out.print("# ");
15 | } else {
16 | System.out.print(" " + " ");
17 | }
18 | }
19 | System.out.println();
20 | }
21 | }
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/src/Pattern/m.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 |
3 | class m {
4 |
5 | public static void main(String args[]) {
6 |
7 | }
8 |
9 | }
--------------------------------------------------------------------------------
/src/Pattern/pattern2.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 |
3 | public class pattern2 {
4 | public static void main(String[] args) {
5 | for (int i = 1; i <= 5; i++) {
6 | for (int j = 1; j <= 5; j++) {
7 |
8 | if (i == 3 && j == 3) {
9 | System.out.print("#");
10 | } else {
11 | System.out.print("*");
12 | }
13 |
14 | }
15 | System.out.println("");
16 |
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/Pattern/pattern3.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 |
3 | public class pattern3 {
4 | public static void main(String[] args) {
5 |
6 | for (int i = 1; i <= 5; i++) {
7 | for (int j = 1; j <= 5; j++) {
8 | System.out.print(" *");
9 | }
10 | System.out.println();
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/src/Pattern/prime.java:
--------------------------------------------------------------------------------
1 | package Pattern;
2 |
3 | public class prime {
4 | public static boolean prime(int a) {
5 | if (a <= 1) {
6 | return false;
7 | }
8 | for (int i = 2; i <= Math.sqrt(a); i++) {
9 | if (a % i == 0) {
10 | return false;
11 | }
12 | }
13 | return true;
14 | }
15 |
16 | public static void main(String[] args) {
17 | System.out.println(prime(4));
18 | }
19 | }
20 | /*
21 | * public class Prime { public static boolean isPrime(int num) { if (num <= 1) {
22 | * return false; }
23 | *
24 | * for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) { return false;
25 | * } }
26 | *
27 | * return true; }
28 | *
29 | * public static void main(String[] args) { for (int i = 0; i <= 50; i++) { if
30 | * (isPrime(i)) { System.out.println(i + " is prime"); } else {
31 | * System.out.println(i + " is not prime"); } } } }
32 | */
33 |
--------------------------------------------------------------------------------
/src/String/CountCharacterOccurrences.java:
--------------------------------------------------------------------------------
1 | package String;
2 |
3 | import java.util.Scanner;
4 |
5 | public class CountCharacterOccurrences {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | System.out.println("Enter a string:");
10 | String inputString = scanner.nextLine();
11 |
12 | System.out.println("Enter the character to count:");
13 | char charToCount = scanner.nextLine().charAt(0);
14 |
15 | int occurrenceCount = countCharacterOccurrences(inputString, charToCount);
16 |
17 | System.out.println("Number of occurrences of '" + charToCount + "': " + occurrenceCount);
18 |
19 | scanner.close();
20 | }
21 |
22 | // Helper method to count the occurrences of a given character in a string
23 | private static int countCharacterOccurrences(String inputString, char charToCount) {
24 | int count = 0;
25 | for (int i = 0; i < inputString.length(); i++) {
26 | if (inputString.charAt(i) == charToCount) {
27 | count++;
28 | }
29 | }
30 | return count;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/String/CountVowelsAndConsonants.java:
--------------------------------------------------------------------------------
1 | package String;
2 |
3 | import java.util.Scanner;
4 |
5 | public class CountVowelsAndConsonants {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 |
9 | System.out.println("Enter a string:");
10 | String inputString = scanner.nextLine().toLowerCase(); // Convert the input string to lowercase for
11 | // case-insensitive counting
12 |
13 | int vowelCount = 0;
14 | int consonantCount = 0;
15 |
16 | for (int i = 0; i < inputString.length(); i++) {
17 | char ch = inputString.charAt(i);
18 | if (Character.isLetter(ch)) { // Check if the character is a letter
19 | if (isVowel(ch)) {
20 | vowelCount++;
21 | } else {
22 | consonantCount++;
23 | }
24 | }
25 | }
26 |
27 | System.out.println("Number of vowels: " + vowelCount);
28 | System.out.println("Number of consonants: " + consonantCount);
29 |
30 | scanner.close();
31 | }
32 |
33 | // Helper method to check if a character is a vowel
34 | private static boolean isVowel(char ch) {
35 | return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/String/DuplicateCharactersFinder.java:
--------------------------------------------------------------------------------
1 | package String;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | public class DuplicateCharactersFinder {
7 |
8 | public static void findDuplicateCharacters(String input) {
9 | Map charCountMap = new HashMap<>();
10 |
11 | // Iterate through each character in the input string
12 | for (char c : input.toCharArray()) {
13 | // Ignore spaces and non-alphabetic characters (modify the condition as needed)
14 | if (Character.isAlphabetic(c)) {
15 | // Convert to lowercase to make the search case-insensitive
16 | char lowercaseChar = Character.toLowerCase(c);
17 |
18 | // Increment the character count in the map
19 | charCountMap.put(lowercaseChar, charCountMap.getOrDefault(lowercaseChar, 0) + 1);
20 | }
21 | }
22 |
23 | // Display the duplicate characters
24 | System.out.println("Duplicate characters in the string:");
25 | for (Map.Entry entry : charCountMap.entrySet()) {
26 | if (entry.getValue() > 1) {
27 | System.out.println(entry.getKey() + " - " + entry.getValue() + " occurrences");
28 | }
29 | }
30 | }
31 |
32 | public static void main(String[] args) {
33 | String inputString = "Hello, World!";
34 | findDuplicateCharacters(inputString);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/String/FirstNonRepeatedCharacter.java:
--------------------------------------------------------------------------------
1 | package String;
2 |
3 | import java.util.LinkedHashMap;
4 | import java.util.Map;
5 |
6 | public class FirstNonRepeatedCharacter {
7 | public static char findFirstNonRepeatedCharacter(String str) {
8 | // Create a LinkedHashMap to store character frequencies while preserving order
9 | Map charFrequencyMap = new LinkedHashMap<>();
10 |
11 | // Calculate character frequencies
12 | for (char ch : str.toCharArray()) {
13 | charFrequencyMap.put(ch, charFrequencyMap.getOrDefault(ch, 0) + 1);
14 | }
15 |
16 | // Find the first non-repeated character
17 | for (char ch : charFrequencyMap.keySet()) {
18 | if (charFrequencyMap.get(ch) == 1) {
19 | return ch;
20 | }
21 | }
22 |
23 | // If no non-repeated character is found, return a default value, such as '\0'
24 | return '\0';
25 | }
26 |
27 | public static void main(String[] args) {
28 | String inputString = "aabbccdeeff";
29 |
30 | char firstNonRepeated = findFirstNonRepeatedCharacter(inputString);
31 | if (firstNonRepeated != '\0') {
32 | System.out.println("The first non-repeated character is: " + firstNonRepeated);
33 | } else {
34 | System.out.println("No non-repeated character found.");
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/String/NameToBinary.java:
--------------------------------------------------------------------------------
1 | package String;
2 |
3 | import java.util.Scanner;
4 |
5 | public class NameToBinary {
6 | public static void main(String[] args) {
7 | Scanner scanner = new Scanner(System.in);
8 | System.out.print("Enter a name: ");
9 | String name = scanner.nextLine();
10 | scanner.close();
11 | String binaryName = nameToBinary(name);
12 | System.out.println("Binary code for '" + name + "': " + binaryName);
13 | }
14 |
15 | public static String nameToBinary(String name) {
16 | StringBuilder binaryCode = new StringBuilder();
17 | for (char ch : name.toCharArray()) {
18 | binaryCode.append(String.format("%8s", Integer.toBinaryString(ch)).replace(' ', '0'));
19 | }
20 | return binaryCode.toString();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/String/StringPermutations.java:
--------------------------------------------------------------------------------
1 | package String;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class StringPermutations {
7 | public static void main(String[] args) {
8 | String inputString = "abc";
9 | List permutations = findPermutations(inputString);
10 | System.out.println(permutations);
11 | }
12 |
13 | public static List findPermutations(String str) {
14 | List result = new ArrayList<>();
15 | findPermutationsHelper("", str, result);
16 | return result;
17 | }
18 |
19 | private static void findPermutationsHelper(String prefix, String suffix, List result) {
20 | int n = suffix.length();
21 | if (n == 0) {
22 | result.add(prefix);
23 | } else {
24 | for (int i = 0; i < n; i++) {
25 | char ch = suffix.charAt(i);
26 | String newPrefix = prefix + ch;
27 | String newSuffix = suffix.substring(0, i) + suffix.substring(i + 1);
28 | findPermutationsHelper(newPrefix, newSuffix, result);
29 | }
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/String/StringPractice1.java:
--------------------------------------------------------------------------------
1 | package String;
2 |
3 | import java.util.Scanner;
4 |
5 | //Find Duplicate Characters in String
6 | public class StringPractice1 {
7 | public static void way1(Scanner scan) {
8 | int count = 0;
9 | String next = scan.nextLine();
10 | System.out.println(next);
11 | for (int i = 0; i < next.length(); i++) {
12 | for (int j = i + 1; j < next.length(); j++) {
13 | char charAt = next.charAt(i);
14 | char charAt2 = next.charAt(j);
15 | if (charAt == charAt2) {
16 | System.out.println(charAt2 + " is duplicate");
17 | count++;
18 | }
19 | }
20 | }
21 | System.out.println(count + "total duplicate");
22 | // this is string work
23 |
24 | }
25 |
26 | public static void main(String[] args) {
27 | Scanner scan = new Scanner(System.in);
28 | way1(scan);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/String/StringPractice2.java:
--------------------------------------------------------------------------------
1 | package String;
2 |
3 | import java.util.Arrays;
4 |
5 | public class StringPractice2 {
6 | public static boolean areAnagrams(String str1, String str2) {
7 |
8 | String s1 = str1.replaceAll("\\s", "").toLowerCase();
9 | String s2 = str2.replaceAll("\\s", "").toLowerCase();
10 |
11 | // Convert to character arrays
12 | char[] arr1 = s1.toCharArray();
13 | char[] arr2 = s2.toCharArray();
14 |
15 | // Sort the arrays
16 | Arrays.sort(arr1);
17 | Arrays.sort(arr2);
18 |
19 | // Compare the sorted arrays
20 | return Arrays.equals(arr1, arr2);
21 | }
22 |
23 | public static void main(String[] args) {
24 | String str1 = "listen";
25 | String str2 = "silent";
26 |
27 | if (areAnagrams(str1, str2)) {
28 | System.out.println("The strings are anagrams.");
29 | } else {
30 | System.out.println("The strings are not anagrams.");
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/String/StringReverseRecursion.java:
--------------------------------------------------------------------------------
1 | package String;
2 |
3 | import java.util.Scanner;
4 |
5 | public class StringReverseRecursion {
6 | public static String reverseString(String str) {
7 | // Base case: If the string is empty or has only one character, return the
8 | // string itself.
9 |
10 | if (str == null || str.length() <= 1) {
11 | return str;
12 | }
13 | //for learning
14 | //interview question
15 | // Recursive case: Reverse the substring from the 2nd character onwards and
16 | // append the first character at the end.
17 | return reverseString(str.substring(1)) + str.charAt(0);
18 | }
19 |
20 | public static void main(String[] args) {
21 | System.out.println("enter string ");
22 | Scanner scanner = new Scanner(System.in);
23 | String inputString = scanner.nextLine();
24 | String reversedString = reverseString(inputString);
25 | System.out.println("Original String: " + inputString);
26 | System.out.println("Reversed String: " + reversedString);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/String/stringprectice4.java:
--------------------------------------------------------------------------------
1 | package String;
2 |
3 | // Check if a String contains only digits?
4 | public class stringprectice4 {
5 | public static boolean containsOnlyDigits(String inputString) {
6 | for (char c : inputString.toCharArray()) {
7 | if (!Character.isDigit(c)) {
8 | return false;
9 | }
10 | }
11 | return true;
12 | }
13 |
14 | public static void main(String[] args) {
15 | String inputStr = "12345";
16 | boolean result = containsOnlyDigits(inputStr);
17 | System.out.println(result); // Output: true
18 |
19 | inputStr = "123abc";
20 | result = containsOnlyDigits(inputStr);
21 | System.out.println(result); // Output: false
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/module-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | /**
5 | * @author manish
6 | *
7 | */
8 | module Data_Structure_and_Algorithm_usingJava {
9 | }
--------------------------------------------------------------------------------