Today's challenges were focused on advanced string manipulation and problem-solving techniques. Below is the overview of the problems tackled on Day 5.
1. Check if Two Strings Are Anagrams
An anagram is formed by rearranging the characters of one string to match another.
import java.util.Scanner;
public class AnagramCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first String: ");
String str1 = sc.nextLine();
System.out.print("Enter secound String: ");
String str2 = sc.nextLine();
if(isAnagram(str1, str2))
System.out.println(str1+" and "+str2+" are Anagrams");
else
System.out.println(str1+" and "+str2+" are not Anagrams");
}
public static boolean isAnagram(String str1, String str2){
boolean bool = false;
if(str1.length()!=str2.length())
return false;
else{
for(char c:str1.toCharArray()){
bool=false;
for(char d:str2.toCharArray()){
if(c==d){
bool=true;
break;
}
}
if(!bool)
return bool;
}
}
return bool;
}
}
2. Find the First Non-Repeating Character in a String
The goal is to identify the first character in a string that does not repeat.
import java.util.Scanner;
public class FirstNonRepeatingCharacter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string: ");
String str = sc.nextLine();
char ch = ' ';
for(int i=0; i<str.length(); i++){
boolean bool = false;;
for(int j=i+1; j<str.length(); j++){
if(str.charAt(i)==str.charAt(j))
bool=true;
}
if(!bool){
ch=str.charAt(i);
break;
}
}
if(ch!=' ')
System.out.println("First non repeating character is: ".concat(Character.toString(ch)));
else
System.out.println("There are no non repeating characters in the given string");
}
}
3. Find All Substrings of a String
Generate and display all possible substrings of a given string.
import java.util.Scanner;
public class AllSubtrings {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the String: ");
String str = sc.nextLine();
for(int i=0; i<str.length(); i++){
for(int j=i; j<str.length(); j++){
String substr = str.substring(i, j+1);
System.out.print(substr.concat(" "));
}
}
}
}
4. Replace Spaces in a String with a Special Character (e.g., %)
Replace all spaces in a string with a specified special character.
import java.util.Scanner;
public class ReplaceSpace {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string: ");
String str = sc.nextLine();
str = str.replaceAll(" ", "%");
System.out.println("New string is: ".concat(str));
}
}
5. Find the Longest Common Prefix in an Array of Strings
Determine the longest common prefix shared among a given array of strings.
import java.util.Scanner;
public class LongestCommonPrefix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of strings: ");
int count = sc.nextInt();
String[] arr = new String[count];
int min_length = Integer.MAX_VALUE;
String substring1 = "";
String answer = "";
System.out.println("Enter the strings: ");
for(int i=0; i<count; i++){
arr[i] = sc.next();
min_length = Math.min(arr[i].length(), min_length);
}
for(int i=0; i<min_length; i++){
substring1 = arr[0].substring(0, i);
boolean bool = true;
for(String j : arr){
String subString2 = j.substring(0, i);
if(!substring1.equals(subString2)){
bool = false;
}
}
if(bool){
answer = answer.length()<substring1.length()?substring1:answer;
}else
break;
}
System.out.println("Longest common prefix: ".concat(answer));
}
}
Reflection
Day 5 introduced some interesting and widely applicable string problems. These challenges emphasized pattern recognition, algorithmic thinking, and efficiency in handling text data. I particularly enjoyed the anagram and longest common prefix problems as they highlight problem-solving skills for interview settings.
Looking forward to more complex challenges on Day 6! ๐