diff --git a/src/test/java/codeWithLogicExplanation/CharacterCount.java b/src/test/java/codeWithLogicExplanation/CharacterCount.java new file mode 100644 index 0000000..2422ce1 --- /dev/null +++ b/src/test/java/codeWithLogicExplanation/CharacterCount.java @@ -0,0 +1,25 @@ +package codeWithLogicExplanation; + +import java.util.HashMap; + +public class CharacterCount { + + public static void main(String[] args) { + + // Input string + String str = "masum"; + + // HashMap to store character as key and its count as value + HashMap map = new HashMap<>(); + + // Loop through each character in the string + for (char c : str.toCharArray()) { + // getOrDefault(c, 0) returns the current count if present, otherwise 0 + // Then we add 1 to update the count + map.put(c, map.getOrDefault(c, 0) + 1); + } + + // Print the final map showing character counts + System.out.println(map); + } +} \ No newline at end of file diff --git a/src/test/java/codeWithLogicExplanation/CharacterCounts.java b/src/test/java/codeWithLogicExplanation/CharacterCounts.java new file mode 100644 index 0000000..0946395 --- /dev/null +++ b/src/test/java/codeWithLogicExplanation/CharacterCounts.java @@ -0,0 +1,30 @@ +package codeWithLogicExplanation; + +import java.util.HashMap; + +public class CharacterCounts { + public static void main(String[] args) { + + // Input string + String str = "masum"; + + // HashMap to store character as key and its frequency as value + HashMap map = new HashMap<>(); + + // Loop through each character in the string + for (char c : str.toCharArray()) { + + // Check if the character already exists in the map + if (map.containsKey(c)) { + // If yes, increment its count + map.put(c, map.get(c) + 1); + } else { + // If not, add it with initial count = 1 + map.put(c, 1); + } + } + + // Print the final map showing character counts + System.out.println(map); + } +} \ No newline at end of file diff --git a/src/test/java/codeWithLogicExplanation/ExpandString.java b/src/test/java/codeWithLogicExplanation/ExpandString.java new file mode 100644 index 0000000..3e950b1 --- /dev/null +++ b/src/test/java/codeWithLogicExplanation/ExpandString.java @@ -0,0 +1,22 @@ +package codeWithLogicExplanation; + +public class ExpandString { + public static void main(String[] args) { + String str = "a2b3c4"; // input string with characters followed by numbers + String result = ""; // final expanded string + + // Loop through the string in steps of 2 (character + digit) + for (int i = 0; i < str.length(); i += 2) { + char ch = str.charAt(i); // the character + int count = Character.getNumericValue(str.charAt(i + 1)); // the number after character + + // Repeat the character 'count' times + for (int j = 1; j <= count; j++) { + result = result + ch; + } + } + + // Print the expanded string + System.out.println(result); + } +} \ No newline at end of file diff --git a/src/test/java/codeWithLogicExplanation/PrimeChecker.java b/src/test/java/codeWithLogicExplanation/PrimeChecker.java new file mode 100644 index 0000000..61f4161 --- /dev/null +++ b/src/test/java/codeWithLogicExplanation/PrimeChecker.java @@ -0,0 +1,30 @@ +package codeWithLogicExplanation; + +public class PrimeChecker { + + public static void main(String[] args) { + // Loop through numbers from 1 to 100 + for (int num = 1; num <= 100; num++) { + // Call isPrime() and print result using ternary operator + System.out.println(num + " is " + (isPrime(num) ? "Prime" : "Not Prime")); + } + } + + // Method to check if a number is prime + private static boolean isPrime(int num) { + + // Prime numbers are greater than 1 + if (num <= 1) + return false; + + // Check divisibility from 2 up to sqrt(num) + // If divisible by any number in this range, it's not prime + for (int i = 2; i <= Math.sqrt(num); i++) { + if (num % i == 0) + return false; + } + + // If no divisors found, it's prime + return true; + } +} \ No newline at end of file diff --git a/src/test/java/codeWithLogicExplanation/ReveseSentnece.java b/src/test/java/codeWithLogicExplanation/ReveseSentnece.java new file mode 100644 index 0000000..01d1f3f --- /dev/null +++ b/src/test/java/codeWithLogicExplanation/ReveseSentnece.java @@ -0,0 +1,31 @@ +package codeWithLogicExplanation; + +public class ReveseSentnece { + public static void main(String[] args) { + // Input string where each word is reversed + String str = "ihled si latipac fo aidnI"; + + // Split the sentence into words using space as delimiter + String words[] = str.split(" "); + + // Variable to store the final reversed sentence + String revSen = ""; + + // Loop through each word + for (String word : words) { + String revWord = ""; // to store reversed word + int len = word.length(); + + // Reverse the current word character by character + for (int i = len - 1; i >= 0; i--) { + revWord = revWord + word.charAt(i); + } + + // Add the reversed word to the sentence + revSen = revSen + revWord + " "; + } + + // Print the final reversed sentence (trim removes extra space at the end) + System.out.println(revSen.trim()); + } +} \ No newline at end of file diff --git a/src/test/java/codeWithLogicExplanation/SecondHigherNumInArray.java b/src/test/java/codeWithLogicExplanation/SecondHigherNumInArray.java new file mode 100644 index 0000000..c1f5407 --- /dev/null +++ b/src/test/java/codeWithLogicExplanation/SecondHigherNumInArray.java @@ -0,0 +1,28 @@ +package codeWithLogicExplanation; + +public class SecondHigherNumInArray { + + public static void main(String[] args) { + + int arr[] = { 112, 34, 553, 35, 23, 678, 876, 366, 53, 757, 3425, 4444 }; + + int first = Integer.MIN_VALUE; // highest number + int second = Integer.MIN_VALUE; // second highest number + + // Loop through each number in the array + for (int num : arr) { + if (num > first) { + // If current number is greater than first, + // then update second as old first, and first as current number + second = first; + first = num; + } else if (num > second && num != first) { + // If current number is greater than second but not equal to first, + // update second + second = num; + } + } + + System.out.println("Second highest number is : " + second); + } +} \ No newline at end of file diff --git a/src/test/java/codeWithLogicExplanation/StringCompresssed.java b/src/test/java/codeWithLogicExplanation/StringCompresssed.java new file mode 100644 index 0000000..8e7334c --- /dev/null +++ b/src/test/java/codeWithLogicExplanation/StringCompresssed.java @@ -0,0 +1,26 @@ +package codeWithLogicExplanation; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class StringCompresssed { + public static void main(String[] args) { + String str = "aaaaaaggggg"; // input string + String result = ""; + + // LinkedHashMap preserves insertion order + Map charc = new LinkedHashMap<>(); + + // Count frequency of each character + for (char c : str.toCharArray()) { + charc.put(c, charc.getOrDefault(c, 0) + 1); + } + + // Build compressed string + for (Map.Entry entry : charc.entrySet()) { + result = result + entry.getKey() + entry.getValue(); + } + + System.out.println(result); + } +} \ No newline at end of file diff --git a/src/test/java/codeWithLogicExplanation/VowelLetterCount.java b/src/test/java/codeWithLogicExplanation/VowelLetterCount.java new file mode 100644 index 0000000..cfca4ba --- /dev/null +++ b/src/test/java/codeWithLogicExplanation/VowelLetterCount.java @@ -0,0 +1,30 @@ +package codeWithLogicExplanation; + +public class VowelLetterCount { + + public static void main(String[] args) { + + // Input string + String input = "rajuiUmbrela"; + + // String containing all vowels (both uppercase and lowercase) + String vowel = "AEIOUaeiou"; + + // Counter to keep track of vowels + int count = 0; + + // Loop through each character of the input string + for (char c : input.toCharArray()) { + + // Check if the character exists in the vowel string + // indexOf(c) returns -1 if the character is not found + if (vowel.indexOf(c) != -1) { + count++; // Increase count if vowel found + System.out.print(c+" "); // Print the vowel character + } + } + + // Print total number of vowels found + System.out.println("\ntotal number of vowels found: "+count); + } +} \ No newline at end of file diff --git a/src/test/java/codeWithLogicExplanation/WhiteSpaceAndWordCount.java b/src/test/java/codeWithLogicExplanation/WhiteSpaceAndWordCount.java new file mode 100644 index 0000000..ad45de2 --- /dev/null +++ b/src/test/java/codeWithLogicExplanation/WhiteSpaceAndWordCount.java @@ -0,0 +1,31 @@ +package codeWithLogicExplanation; + +public class WhiteSpaceAndWordCount { + public static void main(String[] args) { + String str = "hell aa ad ddsa ddd dd"; + + // Count spaces + int count = 0; + for (int i = 0; i <= str.length() - 1; i++) { + if (str.charAt(i) == ' ') { + count++; + } + } + System.out.println("Total white space count is : " + count); + + // Count words + int wordCount = 0; + for (int j = 0; j <= str.length() - 2; j++) { // avoid index out of bounds + // If current char is space AND next char is not space, + // it means a word has ended + if (str.charAt(j) == ' ' && str.charAt(j + 1) != ' ') { + wordCount++; + } + } + + // Add 1 to count the first word (since loop only counts transitions) + wordCount++; + + System.out.println("Total word Count is : " + wordCount); + } +} \ No newline at end of file diff --git a/src/test/java/codeWithLogicExplanation/WhiteSpaceCount.java b/src/test/java/codeWithLogicExplanation/WhiteSpaceCount.java new file mode 100644 index 0000000..39fa5aa --- /dev/null +++ b/src/test/java/codeWithLogicExplanation/WhiteSpaceCount.java @@ -0,0 +1,25 @@ +package codeWithLogicExplanation; + +public class WhiteSpaceCount { + + public static void main(String[] args) { + + // Input string with spaces + String input = "India is a big country"; + + // Counter to keep track of spaces + int count = 0; + + // Loop through each character in the string + for (int i = 0; i <= input.length() - 1; i++) { + + // Check if the current character is a space + if (input.charAt(i) == ' ') { + count++; // Increase count if space found + } + } + + // Print total number of spaces + System.out.println(count); + } +} \ No newline at end of file diff --git a/src/test/java/codeWithLogicExplanation/WordCountExample.java b/src/test/java/codeWithLogicExplanation/WordCountExample.java new file mode 100644 index 0000000..2a97418 --- /dev/null +++ b/src/test/java/codeWithLogicExplanation/WordCountExample.java @@ -0,0 +1,35 @@ +package codeWithLogicExplanation; + +import java.util.HashMap; + +public class WordCountExample { + + public static void main(String[] args) { + + // Input sentence + String sentence = "Interview with aditya CGO Interview"; + + // Convert to lowercase and split by spaces + String[] words = sentence.toLowerCase().split(" "); + + // HashMap to store word as key and frequency as value + HashMap wordCount = new HashMap<>(); + + // Loop through each word + for (String word : words) { + // Check if the word already exists in the map + if (wordCount.containsKey(word)) { + // If yes, increment its count + wordCount.put(word, wordCount.get(word) + 1); + } else { + // If not, add it with initial count = 1 + wordCount.put(word, 1); + } + } + + // Print each word and its frequency + for (String word : wordCount.keySet()) { + System.out.println(word + ": " + wordCount.get(word)); + } + } +} \ No newline at end of file diff --git a/src/test/java/codeWithLogicExplanation/WordDublicate.java b/src/test/java/codeWithLogicExplanation/WordDublicate.java new file mode 100644 index 0000000..251b2f6 --- /dev/null +++ b/src/test/java/codeWithLogicExplanation/WordDublicate.java @@ -0,0 +1,26 @@ +package codeWithLogicExplanation; + +import java.util.HashSet; +import java.util.Set; + +public class WordDublicate { + public static void main(String[] args) { + String Str = "hello ram delhi patna bihar delhi patna bihar"; + + // Split the sentence into words + String[] words = Str.split(" "); + + // HashSet to store unique words + Set uniq = new HashSet<>(); + + // Add each word to the set + for (String word : words) { + uniq.add(word.trim()); // trim removes any accidental spaces + } + + // Print unique words + for (String word : uniq) { + System.out.println(word); + } + } +} \ No newline at end of file diff --git a/target/classes/META-INF/maven/com.interviewPractice2026/interviewPractice2026/pom.properties b/target/classes/META-INF/maven/com.interviewPractice2026/interviewPractice2026/pom.properties index 42fc131..a1b9187 100644 --- a/target/classes/META-INF/maven/com.interviewPractice2026/interviewPractice2026/pom.properties +++ b/target/classes/META-INF/maven/com.interviewPractice2026/interviewPractice2026/pom.properties @@ -1,5 +1,5 @@ #Generated by Maven Integration for Eclipse -#Sun Mar 01 00:25:16 IST 2026 +#Sun Mar 01 00:47:00 IST 2026 artifactId=interviewPractice2026 groupId=com.interviewPractice2026 m2e.projectLocation=D\:\\My_Project\\Testing_Project\\interviewPractice2026 diff --git a/target/test-classes/codeWithLogicExplanation/CharacterCount.class b/target/test-classes/codeWithLogicExplanation/CharacterCount.class new file mode 100644 index 0000000..24a6c7d Binary files /dev/null and b/target/test-classes/codeWithLogicExplanation/CharacterCount.class differ diff --git a/target/test-classes/codeWithLogicExplanation/CharacterCounts.class b/target/test-classes/codeWithLogicExplanation/CharacterCounts.class new file mode 100644 index 0000000..8ac21a6 Binary files /dev/null and b/target/test-classes/codeWithLogicExplanation/CharacterCounts.class differ diff --git a/target/test-classes/codeWithLogicExplanation/ExpandString.class b/target/test-classes/codeWithLogicExplanation/ExpandString.class new file mode 100644 index 0000000..e98dce2 Binary files /dev/null and b/target/test-classes/codeWithLogicExplanation/ExpandString.class differ diff --git a/target/test-classes/codeWithLogicExplanation/PrimeChecker.class b/target/test-classes/codeWithLogicExplanation/PrimeChecker.class new file mode 100644 index 0000000..8ea86f8 Binary files /dev/null and b/target/test-classes/codeWithLogicExplanation/PrimeChecker.class differ diff --git a/target/test-classes/codeWithLogicExplanation/ReveseSentnece.class b/target/test-classes/codeWithLogicExplanation/ReveseSentnece.class new file mode 100644 index 0000000..5c77e92 Binary files /dev/null and b/target/test-classes/codeWithLogicExplanation/ReveseSentnece.class differ diff --git a/target/test-classes/codeWithLogicExplanation/SecondHigherNumInArray.class b/target/test-classes/codeWithLogicExplanation/SecondHigherNumInArray.class new file mode 100644 index 0000000..549e63b Binary files /dev/null and b/target/test-classes/codeWithLogicExplanation/SecondHigherNumInArray.class differ diff --git a/target/test-classes/codeWithLogicExplanation/StringCompresssed.class b/target/test-classes/codeWithLogicExplanation/StringCompresssed.class new file mode 100644 index 0000000..907f7e0 Binary files /dev/null and b/target/test-classes/codeWithLogicExplanation/StringCompresssed.class differ diff --git a/target/test-classes/codeWithLogicExplanation/VowelLetterCount.class b/target/test-classes/codeWithLogicExplanation/VowelLetterCount.class new file mode 100644 index 0000000..13b7b22 Binary files /dev/null and b/target/test-classes/codeWithLogicExplanation/VowelLetterCount.class differ diff --git a/target/test-classes/codeWithLogicExplanation/WhiteSpaceAndWordCount.class b/target/test-classes/codeWithLogicExplanation/WhiteSpaceAndWordCount.class new file mode 100644 index 0000000..988170a Binary files /dev/null and b/target/test-classes/codeWithLogicExplanation/WhiteSpaceAndWordCount.class differ diff --git a/target/test-classes/codeWithLogicExplanation/WhiteSpaceCount.class b/target/test-classes/codeWithLogicExplanation/WhiteSpaceCount.class new file mode 100644 index 0000000..d6bf3c4 Binary files /dev/null and b/target/test-classes/codeWithLogicExplanation/WhiteSpaceCount.class differ diff --git a/target/test-classes/codeWithLogicExplanation/WordCountExample.class b/target/test-classes/codeWithLogicExplanation/WordCountExample.class new file mode 100644 index 0000000..8721043 Binary files /dev/null and b/target/test-classes/codeWithLogicExplanation/WordCountExample.class differ diff --git a/target/test-classes/codeWithLogicExplanation/WordDublicate.class b/target/test-classes/codeWithLogicExplanation/WordDublicate.class new file mode 100644 index 0000000..71a59cd Binary files /dev/null and b/target/test-classes/codeWithLogicExplanation/WordDublicate.class differ