Skip to content

Commit 11650ff

Browse files
committed
Initial commit - String problems
1 parent 7a32876 commit 11650ff

8 files changed

+211
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

STRING/AnagramChecker.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.Arrays;
2+
3+
// Sort both strings.
4+
// If the sorted strings are equal, the original strings are anagrams.
5+
6+
public class AnagramChecker {
7+
8+
public static boolean areAnagrams(String str1, String str2) {
9+
// Convert strings to char arrays and sort them
10+
char[] charArray1 = str1.toCharArray();
11+
char[] charArray2 = str2.toCharArray();
12+
13+
Arrays.sort(charArray1);
14+
Arrays.sort(charArray2);
15+
16+
// Compare the sorted char arrays
17+
return Arrays.equals(charArray1, charArray2);
18+
}
19+
20+
public static void main(String[] args) {
21+
String str1 = "listen";
22+
String str2 = "silent";
23+
24+
// Check if the strings are anagrams
25+
boolean result = areAnagrams(str1, str2);
26+
27+
// Print the result
28+
System.out.println("Are '" + str1 + "' and '" + str2 + "' anagrams? " + result);
29+
}
30+
}

STRING/CharacterCount.java

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// The countOccurrences method takes a string (input) and a target character (targetChar) as parameters.
2+
// It initializes a counter (count) to zero.
3+
// It uses a loop to iterate through each character in the input string.
4+
// Inside the loop, it checks if the current character is equal to the target character, and if so, increments the counter.
5+
// The final count is returned.
6+
7+
public class CharacterCount {
8+
9+
public static int countOccurrences(String input, char targetChar) {
10+
int count = 0;
11+
12+
// Iterate through each character in the string
13+
for (char currentChar : input.toCharArray()) {
14+
// Check if the current character matches the target character
15+
if (currentChar == targetChar) {
16+
count++;
17+
}
18+
}
19+
20+
return count;
21+
}
22+
23+
public static void main(String[] args) {
24+
String inputString = "Hello, World!";
25+
char targetCharacter = 'o';
26+
27+
// Count the occurrences of the target character and print the result
28+
int occurrences = countOccurrences(inputString, targetCharacter);
29+
System.out.println("Number of occurrences of '" + targetCharacter + "': " + occurrences);
30+
}
31+
}

STRING/PalindromeChecker.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class PalindromeChecker {
2+
3+
public static boolean isPalindrome(String input) {
4+
// Remove non-alphanumeric characters and convert to lowercase
5+
String cleanInput = input.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
6+
7+
// Compare the original and reversed strings
8+
return cleanInput.equals(new StringBuilder(cleanInput).reverse().toString());
9+
}
10+
11+
public static void main(String[] args) {
12+
String palindromeString = "A man, a plan, a canal, Panama";
13+
String nonPalindromeString = "Hello, World!";
14+
15+
// Check if the strings are palindromes and print the results
16+
boolean result1 = isPalindrome(palindromeString);
17+
boolean result2 = isPalindrome(nonPalindromeString);
18+
19+
System.out.println("'" + palindromeString + "' is a palindrome: " + result1);
20+
System.out.println("'" + nonPalindromeString + "' is a palindrome: " + result2);
21+
}
22+
}

STRING/RemoveCharacter.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
public class RemoveCharacter {
2+
3+
// public static String removeSpecificCharacter(String input, char charToRemove) {
4+
// StringBuilder result = new StringBuilder();
5+
6+
// for (char currentChar : input.toCharArray()) {
7+
// if (currentChar != charToRemove) {
8+
// result.append(currentChar);
9+
// }
10+
// }
11+
12+
// return result.toString();
13+
// }
14+
15+
public static String removeSpecificCharacter(String input, char charToRemove) {
16+
// Use replace() to remove all occurrences of the specified character
17+
String result = input.replace(String.valueOf(charToRemove), "");
18+
19+
return result;
20+
}
21+
22+
public static String removeFirstOccurrence(String input, char charToRemove) {
23+
// Use replaceFirst() to remove the first occurrence of the specified character
24+
String result = input.replaceFirst(String.valueOf(charToRemove), "");
25+
26+
return result;
27+
}
28+
29+
public static void main(String[] args) {
30+
String originalString = "Hello, World!";
31+
char charToRemove = 'o';
32+
33+
// Remove the specified character and print the result
34+
String stringWithoutChar = removeSpecificCharacter(originalString, charToRemove);
35+
System.out.println("Original: " + originalString);
36+
System.out.println("String without '" + charToRemove + "': " + stringWithoutChar);
37+
}
38+
}

STRING/ReverseString.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class ReverseString {
2+
3+
public static String reverseWithStringBuilder(String input) {
4+
// Use StringBuilder to efficiently reverse the string
5+
return new StringBuilder(input).reverse().toString();
6+
}
7+
8+
public static String reverseWithLoop(String input) {
9+
char[] characters = input.toCharArray();
10+
int left = 0;
11+
int right = characters.length - 1;
12+
13+
// Swap characters from both ends towards the center
14+
while (left < right) {
15+
char temp = characters[left];
16+
characters[left] = characters[right];
17+
characters[right] = temp;
18+
19+
left++;
20+
right--;
21+
}
22+
23+
return new String(characters);
24+
}
25+
26+
public static void main(String[] args) {
27+
String original = "Hello, World!";
28+
29+
// Reverse the string using StringBuilder and print the result
30+
String reversed = reverseWithStringBuilder(original);
31+
System.out.println("Original: " + original);
32+
System.out.println("Reversed: " + reversed);
33+
}
34+
}

STRING/StringPermutations.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Explanation:
2+
3+
// printPermutations Method:
4+
// This is the public method that initializes the recursive process.
5+
6+
// printPermutationsHelper Method:
7+
// The recursive helper method generates permutations by swapping characters.
8+
// It prints the current permutation when the start index reaches the end of the string.
9+
10+
// swap Method:
11+
// A helper method to swap characters in the array.
12+
13+
14+
public class StringPermutations {
15+
16+
public static void printPermutations(String str) {
17+
printPermutationsHelper(str.toCharArray(), 0, str.length() - 1);
18+
}
19+
20+
private static void printPermutationsHelper(char[] chars, int start, int end) {
21+
if (start == end) {
22+
System.out.println(new String(chars));
23+
} else {
24+
for (int i = start; i <= end; i++) {
25+
swap(chars, start, i);
26+
printPermutationsHelper(chars, start + 1, end);
27+
swap(chars, start, i); // Backtrack to restore the original order
28+
}
29+
}
30+
}
31+
32+
private static void swap(char[] chars, int i, int j) {
33+
char temp = chars[i];
34+
chars[i] = chars[j];
35+
chars[j] = temp;
36+
}
37+
38+
public static void main(String[] args) {
39+
String input = "abc";
40+
System.out.println("All Permutations of " + input + ":");
41+
printPermutations(input);
42+
}
43+
}

STRING/TotatCharacters.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class TotatCharacters {
2+
public static void main(String[] args) {
3+
String str = "Scaler by InterviewBit";
4+
int count = 0;
5+
System.out.println("Input String: " + str);
6+
//Count total characters in the given string except space
7+
for (int i = 0; i < str.length(); i++) {
8+
if (str.charAt(i) != ' ') count++;
9+
}
10+
//Display total number of characters in the given string
11+
System.out.println("The total number of characters in the given string: " + coun
12+
}
13+
}

0 commit comments

Comments
 (0)