1.
Program to reverse a string:
import [Link];
public class ReverseString {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
String reversed = reverseString(input);
[Link]("Reversed string: " + reversed);
}
private static String reverseString(String str) {
char[] chars = [Link]();
int start = 0;
int end = [Link]() - 1;
while (start < end) {
char temp = chars[start];
chars[start] = chars[end];
chars[end] = temp;
start++;
end--;
}
return new String(chars);
}
}
[Link] to check if a string is a palindrome:
import [Link];
public class PalindromeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
if (isPalindrome(input)) {
[Link]("The string is a palindrome.");
} else {
[Link]("The string is not a palindrome.");
}
}
private static boolean isPalindrome(String str) {
int start = 0;
int end = [Link]() - 1;
while (start < end) {
if ([Link](start) != [Link](end)) {
return false;
}
start++;
end--;
}
return true;
}
}
[Link] to count the number of words in a string:
import [Link];
public class WordCount {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
int wordCount = countWords(input);
[Link]("Number of words in the string: " + wordCount);
}
private static int countWords(String str) {
int count = 0;
boolean isWord = false;
for (char c : [Link]()) {
if ([Link](c)) {
if (!isWord) {
isWord = true;
count++;
}
} else {
isWord = false;
}
}
return count;
}
}
[Link] to check if two strings are anagrams:
import [Link];
public class AnagramCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter first string: ");
String str1 = [Link]();
[Link]("Enter second string: ");
String str2 = [Link]();
if (areAnagrams(str1, str2)) {
[Link]("The strings are anagrams.");
} else {
[Link]("The strings are not anagrams.");
}
}
private static boolean areAnagrams(String str1, String str2) {
if ([Link]() != [Link]()) {
return false;
}
int[] count = new int[256];
for (int i = 0; i < [Link](); i++) {
count[[Link](i)]++;
count[[Link](i)]--;
}
for (int i = 0; i < 256; i++) {
if (count[i] != 0) {
return false;
}
}
return true;
}
}
[Link] to find the longest substring without repeating characters:
import [Link];
public class LongestSubstringWithoutRepeating {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
int length = longestSubstringWithoutRepeating(str);
[Link]("Length of the longest substring without repeating characters: " +
length);
}
private static int longestSubstringWithoutRepeating(String str) {
int maxLength = 0;
int[] lastIndex = new int[256];
int start = 0;
for (int end = 0; end < [Link](); end++) {
start = [Link](start, lastIndex[[Link](end)]);
maxLength = [Link](maxLength, end - start + 1);
lastIndex[[Link](end)] = end + 1;
}
return maxLength;
}
}
[Link] to perform string compression by counting repeated characters:
import [Link];
public class StringCompression {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String compressed = compressString(str);
[Link]("Compressed string: " + compressed);
}
private static String compressString(String str) {
StringBuilder compressed = new StringBuilder();
int count = 1;
for (int i = 0; i < [Link](); i++) {
if (i + 1 < [Link]() && [Link](i) == [Link](i + 1)) {
count++;
} else {
[Link]([Link](i)).append(count);
count = 1;
}
}
return [Link]();
}
}
[Link] to check if a string is a pangram:
import [Link];
public class PangramCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]().toLowerCase();
if (isPangram(str)) {
[Link]("The string is a pangram.");
} else {
[Link]("The string is not a pangram.");
}
}
private static boolean isPangram(String str) {
boolean[] visited = new boolean[26];
int count = 0;
for (int i = 0; i < [Link](); i++) {
char ch = [Link](i);
if ('a' <= ch && ch <= 'z' && !visited[ch - 'a']) {
visited[ch - 'a'] = true;
count++;
}
if (count == 26) {
return true;
}
}
return false;
}
}
[Link] to find all permutations of a string:
import [Link];
public class StringPermutations {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
[Link]("Permutations of the string:");
permute(str, 0, [Link]() - 1);
}
private static void permute(String str, int l, int r) {
if (l == r) {
[Link](str);
} else {
for (int i = l; i <= r; i++) {
str = swap(str, l, i);
permute(str, l + 1, r);
str = swap(str, l, i);
}
}
}
private static String swap(String str, int i, int j) {
char[] charArray = [Link]();
char temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return [Link](charArray);
}
}
[Link] to count the occurrences of each character in a string:
import [Link];
public class CharacterCount {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
countCharacters(str);
}
private static void countCharacters(String str) {
int[] count = new int[256];
for (int i = 0; i < [Link](); i++) {
count[[Link](i)]++;
}
[Link]("Character counts:");
for (int i = 0; i < 256; i++) {
if (count[i] > 0) {
[Link]((char) i + ": " + count[i]);
}
}
}
}
[Link] to check if a string is a valid shuffle of two other strings:
import [Link];
public class ValidShuffle {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter first string: ");
String str1 = [Link]();
[Link]("Enter second string: ");
String str2 = [Link]();
[Link]("Enter shuffled string: ");
String shuffled = [Link]();
if (isValidShuffle(str1, str2, shuffled)) {
[Link]("The shuffled string is valid.");
} else {
[Link]("The shuffled string is not valid.");
}
}
private static boolean isValidShuffle(String str1, String str2, String shuffled) {
if ([Link]() + [Link]() != [Link]()) {
return false;
}
int i = 0, j = 0, k = 0;
while (k < [Link]()) {
if (i < [Link]() && [Link](i) == [Link](k)) {
i++;
} else if (j < [Link]() && [Link](j) == [Link](k)) {
j++;
} else {
return false;
}
k++;
}
return true;
}
}
[Link] to remove duplicate characters from a string:
import [Link];
public class RemoveDuplicates {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String result = removeDuplicates(str);
[Link]("String after removing duplicates: " + result);
}
private static String removeDuplicates(String str) {
boolean[] seen = new boolean[256];
StringBuilder sb = new StringBuilder();
for (char c : [Link]()) {
if (!seen[c]) {
seen[c] = true;
[Link](c);
}
}
return [Link]();
}
}
[Link] to find the longest common prefix among an array of strings:
import [Link];
public class LongestCommonPrefix {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of strings: ");
int n = [Link]();
String[] strings = new String[n];
[Link](); // Consume newline
for (int i = 0; i < n; i++) {
[Link]("Enter string " + (i + 1) + ": ");
strings[i] = [Link]();
}
String prefix = longestCommonPrefix(strings);
[Link]("Longest common prefix: " + prefix);
}
private static String longestCommonPrefix(String[] strings) {
if (strings == null || [Link] == 0) {
return "";
}
String prefix = strings[0];
for (int i = 1; i < [Link]; i++) {
while (strings[i].indexOf(prefix) != 0) {
prefix = [Link](0, [Link]() - 1);
if ([Link]()) {
return "";
}
}
}
return prefix;
}
}
[Link] to check if a string is a valid palindrome ignoring non-alphanumeric characters:
import [Link];
public class ValidPalindrome {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]().toLowerCase();
if (isValidPalindrome(str)) {
[Link]("The string is a valid palindrome.");
} else {
[Link]("The string is not a valid palindrome.");
}
}
private static boolean isValidPalindrome(String str) {
int left = 0, right = [Link]() - 1;
while (left < right) {
while (left < right && )) {
left++;
}
while (left < right && )) {
right--;
}
if (left < right && [Link](left) != [Link](right)) {
return false;
}
left++;
right--;
}
return true;
}
}
[Link] to find the first non-repeating character in a string:
import [Link];
public class FirstNonRepeatingCharacter {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
char firstNonRepeating = findFirstNonRepeating(str);
if (firstNonRepeating != '\0') {
[Link]("First non-repeating character: " + firstNonRepeating);
} else {
[Link]("No non-repeating character found.");
}
}
private static char findFirstNonRepeating(String str) {
int[] count = new int[256];
for (char c : [Link]()) {
count[c]++;
}
for (char c : [Link]()) {
if (count[c] == 1) {
return c;
}
}
return '\0';
}
}
[Link] to check if a string contains only digits:
import [Link];
public class OnlyDigitsCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
if (containsOnlyDigits(str)) {
[Link]("The string contains only digits.");
} else {
[Link]("The string does not contain only digits.");
}
}
private static boolean containsOnlyDigits(String str) {
for (char c : [Link]()) {
if () {
return false;
}
}
return true;
}
}
16. Program to find the longest substring without repeating characters:
import [Link];
public class LongestSubstringWithoutRepeating {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String longestSubstring = findLongestSubstringWithoutRepeating(str);
[Link]("Longest substring without repeating characters: " + longestSubstring);
}
private static String findLongestSubstringWithoutRepeating(String str) {
int start = 0, end = 0;
int maxLength = 0;
int[] lastIndex = new int[256];
for (int i = 0; i < [Link]; i++) {
lastIndex[i] = -1;
}
int currentLength = 0;
int longestStart = 0;
for (int i = 0; i < [Link](); i++) {
int index = [Link](i);
if (lastIndex[index] == -1 || i - lastIndex[index] > currentLength) {
currentLength++;
} else {
currentLength = i - lastIndex[index];
start = lastIndex[index] + 1;
}
lastIndex[index] = i;
if (currentLength > maxLength) {
maxLength = currentLength;
longestStart = start;
}
}
return [Link](longestStart, longestStart + maxLength);
}
}
[Link] to reverse words in a sentence:
import [Link];
public class ReverseWordsInSentence {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a sentence: ");
String sentence = [Link]();
String reversedSentence = reverseWords(sentence);
[Link]("Reversed sentence: " + reversedSentence);
}
private static String reverseWords(String sentence) {
String[] words = [Link](" ");
StringBuilder reversed = new StringBuilder();
for (int i = [Link] - 1; i >= 0; i--) {
[Link](words[i]).append(" ");
}
return [Link]().trim();
}
}
[Link] to find all permutations of a string:
import [Link];
public class StringPermutations {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
[Link]("Permutations of the string:");
generatePermutations(str, 0, [Link]() - 1);
}
private static void generatePermutations(String str, int left, int right) {
if (left == right) {
[Link](str);
} else {
for (int i = left; i <= right; i++) {
str = swap(str, left, i);
generatePermutations(str, left + 1, right);
str = swap(str, left, i);
}
}
}
private static String swap(String str, int i, int j) {
char[] charArray = [Link]();
char temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return new String(charArray);
}
}
[Link] to find the frequency of each character in a string:
import [Link];
public class CharacterFrequency {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
countCharacterFrequency(str);
}
private static void countCharacterFrequency(String str) {
int[] frequency = new int[256];
for (char c : [Link]()) {
frequency[c]++;
}
[Link]("Character frequency:");
for (int i = 0; i < 256; i++) {
if (frequency[i] > 0) {
[Link]((char) i + ": " + frequency[i]);
}
}
}
}
[Link] to check if two strings are rotations of each other:
import [Link];
public class StringRotationCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
if (areRotations(str1, str2)) {
[Link]("The strings are rotations of each other.");
} else {
[Link]("The strings are not rotations of each other.");
}
}
private static boolean areRotations(String str1, String str2) {
return [Link]() == [Link]() && (str1 + str1).contains(str2);
}
}
[Link] to find the first non-repeating character in a string:
import [Link];
import [Link];
import [Link];
public class FirstNonRepeatingCharacter {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
char firstNonRepeating = findFirstNonRepeating(str);
if (firstNonRepeating != '\0') {
[Link]("First non-repeating character: " + firstNonRepeating);
} else {
[Link]("No non-repeating character found.");
}
}
private static char findFirstNonRepeating(String str) {
Map<Character, Integer> countMap = new HashMap<>();
for (char c : [Link]()) {
[Link](c, [Link](c, 0) + 1);
}
for (char c : [Link]()) {
if ([Link](c) == 1) {
return c;
}
}
return '\0';
}
}
[Link] to find the longest palindrome substring in a string:
import [Link];
public class LongestPalindromeSubstring {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String longestPalindrome = findLongestPalindromeSubstring(str);
[Link]("Longest palindrome substring: " + longestPalindrome);
}
private static String findLongestPalindromeSubstring(String str) {
int maxLength = 0;
String longestPalindrome = "";
for (int i = 0; i < [Link](); i++) {
for (int j = i + 1; j <= [Link](); j++) {
String substring = [Link](i, j);
if (isPalindrome(substring) && [Link]() > maxLength) {
maxLength = [Link]();
longestPalindrome = substring;
}
}
}
return longestPalindrome;
}
private static boolean isPalindrome(String str) {
int left = 0, right = [Link]() - 1;
while (left < right) {
if ([Link](left) != [Link](right)) {
return false;
}
left++;
right--;
}
return true;
}
}
[Link] to check if a string is an anagram of a palindrome:
import [Link];
public class AnagramPalindromeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
if (isAnagramOfPalindrome(str)) {
[Link]("The string is an anagram of a palindrome.");
} else {
[Link]("The string is not an anagram of a palindrome.");
}
}
private static boolean isAnagramOfPalindrome(String str) {
int[] count = new int[256];
for (char c : [Link]()) {
count[c]++;
}
int oddCount = 0;
for (int frequency : count) {
if (frequency % 2 != 0) {
oddCount++;
}
}
return oddCount <= 1;
}
}
[Link] to find the minimum number of characters to be inserted to make a string
palindrome:
import [Link];
public class MinInsertionsForPalindrome {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
int minInsertions = findMinInsertionsForPalindrome(str);
[Link]("Minimum insertions required: " + minInsertions);
}
private static int findMinInsertionsForPalindrome(String str) {
int[][] dp = new int[[Link]()][[Link]()];
for (int gap = 1; gap < [Link](); gap++) {
for (int i = 0, j = gap; j < [Link](); i++, j++) {
dp[i][j] = ([Link](i) == [Link](j)) ? dp[i + 1][j - 1]
: [Link](dp[i][j - 1], dp[i + 1][j]) + 1;
}
}
return dp[0][[Link]() - 1];
}
}
[Link] to check if a string is a valid shuffle of two other strings:
import [Link];
public class ValidShuffle {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter first string: ");
String str1 = [Link]();
[Link]("Enter second string: ");
String str2 = [Link]();
[Link]("Enter shuffled string: ");
String shuffled = [Link]();
if (isValidShuffle(str1, str2, shuffled)) {
[Link]("The shuffled string is valid.");
} else {
[Link]("The shuffled string is not valid.");
}
}
private static boolean isValidShuffle(String str1, String str2, String shuffled) {
if ([Link]() + [Link]() != [Link]()) {
return false;
}
int i = 0, j = 0, k = 0;
while (k < [Link]()) {
if (i < [Link]() && [Link](i) == [Link](k)) {
i++;
} else if (j < [Link]() && [Link](j) == [Link](k)) {
j++;
} else {
return false;
}
k++;
}
return true;
}
}
[Link] to find the maximum occurring character in a string:
import [Link];
public class MaxOccurringCharacter {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
char maxOccurring = findMaxOccurringCharacter(str);
[Link]("Maximum occurring character: " + maxOccurring);
}
private static char findMaxOccurringCharacter(String str) {
int[] frequency = new int[256];
for (char c : [Link]()) {
frequency[c]++;
}
char maxChar = '\0';
int maxFrequency = 0;
for (int i = 0; i < [Link]; i++) {
if (frequency[i] > maxFrequency) {
maxFrequency = frequency[i];
maxChar = (char) i;
}
}
return maxChar;
}
}
[Link] to reverse words in a sentence:
import [Link];
public class ReverseWordsInSentence {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a sentence: ");
String sentence = [Link]();
String reversedSentence = reverseWords(sentence);
[Link]("Reversed sentence: " + reversedSentence);
}
private static String reverseWords(String sentence) {
StringBuilder reversed = new StringBuilder();
String[] words = [Link]().split("\\s+");
for (int i = [Link] - 1; i >= 0; i--) {
[Link](words[i]);
if (i > 0) {
[Link](" ");
}
}
return [Link]();
}
}
[Link] to find the length of the last word in a string:
import [Link];
public class LengthOfLastWord {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
int length = lengthOfLastWord(str);
[Link]("Length of the last word: " + length);
}
private static int lengthOfLastWord(String str) {
int length = 0;
boolean wordStarted = false;
for (int i = [Link]() - 1; i >= 0; i--) {
if ([Link]([Link](i))) {
length++;
wordStarted = true;
} else if (wordStarted) {
break;
}
}
return length;
}
}
[Link] to count the number of vowels and consonants in a string:
import [Link];
public class VowelConsonantCount {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]().toLowerCase();
int vowels = 0, consonants = 0;
for (char c : [Link]()) {
if (c >= 'a' && c <= 'z') {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
vowels++;
} else {
consonants++;
}
}
}
[Link]("Number of vowels: " + vowels);
[Link]("Number of consonants: " + consonants);
}
}
[Link] to check if a string is a rotation of another string:
import [Link];
public class RotationCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
if (isRotation(str1, str2)) {
[Link]("The strings are rotations of each other.");
} else {
[Link]("The strings are not rotations of each other.");
}
}
private static boolean isRotation(String str1, String str2) {
return ([Link]() == [Link]()) && ((str1 + str1).contains(str2));
}
}
[Link] to check if a string is a palindrome:
import [Link];
public class PalindromeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
if (isPalindrome(str)) {
[Link]("The string is a palindrome.");
} else {
[Link]("The string is not a palindrome.");
}
}
private static boolean isPalindrome(String str) {
int left = 0, right = [Link]() - 1;
while (left < right) {
if ([Link](left) != [Link](right)) {
return false;
}
left++;
right--;
}
return true;
}
}
[Link] to check if a string contains only unique characters:
import [Link];
public class UniqueCharactersCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
if (hasUniqueCharacters(str)) {
[Link]("The string contains only unique characters.");
} else {
[Link]("The string contains duplicate characters.");
}
}
private static boolean hasUniqueCharacters(String str) {
boolean[] visited = new boolean[256];
for (char c : [Link]()) {
if (visited[c]) {
return false;
}
visited[c] = true;
}
return true;
}
}
[Link] to convert a string to title case:
import [Link];
public class TitleCaseConversion {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String titleCase = convertToTitleCase(str);
[Link]("Title case string: " + titleCase);
}
private static String convertToTitleCase(String str) {
StringBuilder result = new StringBuilder();
boolean capitalizeNext = true;
for (char c : [Link]()) {
if ([Link](c)) {
capitalizeNext = true;
[Link](c);
} else {
if (capitalizeNext) {
[Link]([Link](c));
capitalizeNext = false;
} else {
[Link]([Link](c));
}
}
}
return [Link]();
}
}
[Link] to check if a string is an anagram of another string:
import [Link];
public class AnagramCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
if (areAnagrams(str1, str2)) {
[Link]("The strings are anagrams.");
} else {
[Link]("The strings are not anagrams.");
}
}
private static boolean areAnagrams(String str1, String str2) {
if ([Link]() != [Link]()) {
return false;
}
int[] count = new int[256];
for (char c : [Link]()) {
count[c]++;
}
for (char c : [Link]()) {
if (count[c] == 0) {
return false;
}
count[c]--;
}
return true;
}
}
[Link] to check if a string is a rotation of another string:
import [Link];
public class RotationCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
if (areRotations(str1, str2)) {
[Link]("The strings are rotations of each other.");
} else {
[Link]("The strings are not rotations of each other.");
}
}
private static boolean areRotations(String str1, String str2) {
if ([Link]() != [Link]()) {
return false;
}
String concatenated = str1 + str1;
return [Link](str2);
}
}
[Link] to check if a string is a valid shuffle of two other strings:
import [Link];
public class ValidShuffleCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
[Link]("Enter the shuffled string: ");
String shuffled = [Link]();
if (isValidShuffle(str1, str2, shuffled)) {
[Link]("The shuffled string is valid.");
} else {
[Link]("The shuffled string is not valid.");
}
}
private static boolean isValidShuffle(String str1, String str2, String shuffled) {
if ([Link]() + [Link]() != [Link]()) {
return false;
}
int i = 0, j = 0, k = 0;
while (k < [Link]()) {
if (i < [Link]() && [Link](i) == [Link](k)) {
i++;
} else if (j < [Link]() && [Link](j) == [Link](k)) {
j++;
} else {
return false;
}
k++;
}
return true;
}
}
[Link] to find the longest common prefix among an array of strings:
import [Link];
public class LongestCommonPrefix {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of strings: ");
int n = [Link]();
[Link](); // Consume newline
String[] strings = new String[n];
for (int i = 0; i < n; i++) {
[Link]("Enter string " + (i + 1) + ": ");
strings[i] = [Link]();
}
String prefix = longestCommonPrefix(strings);
[Link]("Longest common prefix: " + prefix);
}
private static String longestCommonPrefix(String[] strings) {
if (strings == null || [Link] == 0) {
return "";
}
String prefix = strings[0];
for (int i = 1; i < [Link]; i++) {
while (strings[i].indexOf(prefix) != 0) {
prefix = [Link](0, [Link]() - 1);
if ([Link]()) {
return "";
}
}
}
return prefix;
}
}
[Link] to count the number of words in a sentence:
import [Link];
public class WordCount {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a sentence: ");
String sentence = [Link]();
int wordCount = countWords(sentence);
[Link]("Number of words: " + wordCount);
}
private static int countWords(String sentence) {
String[] words = [Link]().split("\\s+");
return [Link];
}
}
[Link] to find the first non-repeating character in a string:
import [Link];
public class FirstNonRepeatingCharacter {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
char firstNonRepeating = findFirstNonRepeating(str);
if (firstNonRepeating != '\0') {
[Link]("First non-repeating character: " + firstNonRepeating);
} else {
[Link]("No non-repeating character found.");
}
}
private static char findFirstNonRepeating(String str) {
int[] count = new int[256];
for (char c : [Link]()) {
count[c]++;
}
for (char c : [Link]()) {
if (count[c] == 1) {
return c;
}
}
return '\0';
}
}
[Link] to check if a string is a valid shuffle of two other strings:
import [Link];
public class ValidShuffleCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
[Link]("Enter the shuffled string: ");
String shuffled = [Link]();
if (isValidShuffle(str1, str2, shuffled)) {
[Link]("The shuffled string is valid.");
} else {
[Link]("The shuffled string is not valid.");
}
}
private static boolean isValidShuffle(String str1, String str2, String shuffled) {
if ([Link]() + [Link]() != [Link]()) {
return false;
}
int i = 0, j = 0, k = 0;
while (k < [Link]()) {
if (i < [Link]() && [Link](i) == [Link](k)) {
i++;
} else if (j < [Link]() && [Link](j) == [Link](k)) {
j++;
} else {
return false;
}
k++;
}
return true;
}
}
[Link] to count the occurrences of a substring in a string:
import [Link];
public class SubstringCount {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the main string: ");
String mainString = [Link]();
[Link]("Enter the substring to count: ");
String subString = [Link]();
int count = countSubstring(mainString, subString);
[Link]("Occurrences of substring: " + count);
}
private static int countSubstring(String mainString, String subString) {
int count = 0;
int index = [Link](subString);
while (index != -1) {
count++;
index = [Link](subString, index + 1);
}
return count;
}
}
[Link] to reverse a string without using a built-in function:
import [Link];
public class StringReversal {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String reversed = reverseString(str);
[Link]("Reversed string: " + reversed);
}
private static String reverseString(String str) {
StringBuilder reversed = new StringBuilder();
for (int i = [Link]() - 1; i >= 0; i--) {
[Link]([Link](i));
}
return [Link]();
}
}
[Link] to find the duplicate characters in a string:
import [Link];
import [Link];
import [Link];
public class DuplicateCharacters {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
Map<Character, Integer> charCount = findDuplicateCharacters(str);
[Link]("Duplicate characters:");
for ([Link]<Character, Integer> entry : [Link]()) {
if ([Link]() > 1) {
[Link]([Link]() + " - " + [Link]() + " times");
}
}
}
private static Map<Character, Integer> findDuplicateCharacters(String str) {
Map<Character, Integer> charCount = new HashMap<>();
for (char c : [Link]()) {
[Link](c, [Link](c, 0) + 1);
}
return charCount;
}
}
[Link] to check if a string is a pangram:
import [Link];
public class PangramCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]().toLowerCase();
if (isPangram(str)) {
[Link]("The string is a pangram.");
} else {
[Link]("The string is not a pangram.");
}
}
private static boolean isPangram(String str) {
boolean[] alphabet = new boolean[26];
for (char c : [Link]()) {
if ([Link](c)) {
alphabet[c - 'a'] = true;
}
}
for (boolean present : alphabet) {
if (!present) {
return false;
}
}
return true;
}
}
[Link] to remove duplicates from a string:
import [Link];
public class RemoveDuplicates {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String removedDuplicates = removeDuplicates(str);
[Link]("String after removing duplicates: " + removedDuplicates);
}
private static String removeDuplicates(String str) {
StringBuilder result = new StringBuilder();
boolean[] visited = new boolean[256];
for (char c : [Link]()) {
if (!visited[c]) {
visited[c] = true;
[Link](c);
}
}
return [Link]();
}
}
[Link] to find the longest substring without repeating characters:
import [Link];
import [Link];
import [Link];
public class LongestSubstringWithoutRepeating {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
int maxLength = longestSubstringWithoutRepeating(str);
[Link]("Length of the longest substring without repeating characters: " +
maxLength);
}
private static int longestSubstringWithoutRepeating(String str) {
int maxLength = 0;
Set<Character> set = new HashSet<>();
int i = 0, j = 0;
while (j < [Link]()) {
if ()) {
[Link]([Link](j++));
maxLength = [Link](maxLength, j - i);
} else {
[Link]([Link](i++));
}
}
return maxLength;
}
}
[Link] to check if a string is a valid shuffle of two other strings:
import [Link];
public class ValidShuffleCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
[Link]("Enter the shuffled string: ");
String shuffled = [Link]();
if (isValidShuffle(str1, str2, shuffled)) {
[Link]("The shuffled string is valid.");
} else {
[Link]("The shuffled string is not valid.");
}
}
private static boolean isValidShuffle(String str1, String str2, String shuffled) {
if ([Link]() + [Link]() != [Link]()) {
return false;
}
int i = 0, j = 0, k = 0;
while (k < [Link]()) {
if (i < [Link]() && [Link](i) == [Link](k)) {
i++;
} else if (j < [Link]() && [Link](j) == [Link](k)) {
j++;
} else {
return false;
}
k++;
}
return true;
}
}
[Link] to count the number of palindromic substrings in a string:
import [Link];
public class PalindromicSubstringCount {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
int count = countPalindromicSubstrings(str);
[Link]("Number of palindromic substrings: " + count);
}
private static int countPalindromicSubstrings(String str) {
int count = 0;
for (int i = 0; i < [Link](); i++) {
count += countPalindromesFromCenter(str, i, i); // Odd length palindromes
count += countPalindromesFromCenter(str, i, i + 1); // Even length palindromes
}
return count;
}
private static int countPalindromesFromCenter(String str, int left, int right) {
int count = 0;
while (left >= 0 && right < [Link]() && [Link](left) == [Link](right)) {
count++;
left--;
right++;
}
return count;
}
}
[Link] to find all permutations of a string:
import [Link];
public class StringPermutations {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
[Link]("Permutations of the string:");
generatePermutations("", str);
}
private static void generatePermutations(String prefix, String remaining) {
int n = [Link]();
if (n == 0) {
[Link](prefix);
} else {
for (int i = 0; i < n; i++) {
generatePermutations(prefix + [Link](i), [Link](0, i) +
[Link](i + 1));
}
}
}
}
[Link] to check if two strings are anagrams of each other (without using sorting):
import [Link];
public class AnagramCheckWithoutSorting {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
if (areAnagrams(str1, str2)) {
[Link]("The strings are anagrams.");
} else {
[Link]("The strings are not anagrams.");
}
}
private static boolean areAnagrams(String str1, String str2) {
if ([Link]() != [Link]()) {
return false;
}
int[] count = new int[256];
for (char c : [Link]()) {
count[c]++;
}
for (char c : [Link]()) {
if (count[c] == 0) {
return false;
}
count[c]--;
}
return true;
}
}
[Link] to check if a string is a valid shuffle of two other strings:
import [Link];
public class ValidShuffleCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
[Link]("Enter the shuffled string: ");
String shuffled = [Link]();
if (isValidShuffle(str1, str2, shuffled)) {
[Link]("The shuffled string is valid.");
} else {
[Link]("The shuffled string is not valid.");
}
}
private static boolean isValidShuffle(String str1, String str2, String shuffled) {
if ([Link]() + [Link]() != [Link]()) {
return false;
}
int i = 0, j = 0, k = 0;
while (k < [Link]()) {
if (i < [Link]() && [Link](i) == [Link](k)) {
i++;
} else if (j < [Link]() && [Link](j) == [Link](k)) {
j++;
} else {
return false;
}
k++;
}
return true;
}
}
[Link] to find the longest palindrome substring in a string:
import [Link];
public class LongestPalindromeSubstring {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String longestPalindrome = findLongestPalindrome(str);
[Link]("Longest palindrome substring: " + longestPalindrome);
}
private static String findLongestPalindrome(String str) {
int maxLength = 1;
int start = 0;
for (int i = 0; i < [Link](); i++) {
for (int j = i; j < [Link](); j++) {
if (isPalindrome([Link](i, j + 1)) && (j - i + 1) > maxLength) {
start = i;
maxLength = j - i + 1;
}
}
}
return [Link](start, start + maxLength);
}
private static boolean isPalindrome(String str) {
int left = 0, right = [Link]() - 1;
while (left < right) {
if ([Link](left++) != [Link](right--)) {
return false;
}
}
return true;
}
}
[Link] to convert a string to lowercase without using built-in functions:
import [Link];
public class ToLowerCase {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String lowercase = convertToLowerCase(str);
[Link]("Lowercase string: " + lowercase);
}
private static String convertToLowerCase(String str) {
StringBuilder result = new StringBuilder();
for (char c : [Link]()) {
if (c >= 'A' && c <= 'Z') {
[Link]((char) (c + 32));
} else {
[Link](c);
}
}
return [Link]();
}
}
[Link] to reverse words in a sentence without using built-in functions:
import [Link];
public class ReverseWordsInSentence {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a sentence: ");
String sentence = [Link]();
String reversedSentence = reverseWords(sentence);
[Link]("Reversed sentence: " + reversedSentence);
}
private static String reverseWords(String sentence) {
StringBuilder reversed = new StringBuilder();
int end = [Link]();
for (int i = [Link]() - 1; i >= 0; i--) {
if ([Link](i) == ' ') {
[Link]([Link](i + 1, end)).append(" ");
end = i;
}
}
[Link]([Link](0, end));
return [Link]();
}
}
[Link] to check if a string is a valid palindrome ignoring non-alphanumeric characters:
import [Link];
public class ValidPalindromeIgnoreNonAlphaNumeric {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
if (isValidPalindromeIgnoringNonAlphaNumeric(str)) {
[Link]("The string is a valid palindrome ignoring non-alphanumeric
characters.");
} else {
[Link]("The string is not a valid palindrome ignoring non-alphanumeric
characters.");
}
}
private static boolean isValidPalindromeIgnoringNonAlphaNumeric(String str) {
int left = 0, right = [Link]() - 1;
while (left < right) {
while (left < right && )) {
left++;
}
while (left < right && )) {
right--;
}
if (left < right && [Link]([Link](left)) !=
[Link]([Link](right))) {
return false;
}
left++;
right--;
}
return true;
}
}
[Link] to count the occurrences of each character in a string:
import [Link];
public class CharacterCount {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
countCharacters(str);
}
private static void countCharacters(String str) {
int[] count = new int[256]; // Assuming ASCII characters
for (char c : [Link]()) {
count[c]++;
}
for (int i = 0; i < [Link]; i++) {
if (count[i] > 0) {
[Link]((char) i + " - " + count[i] + " times");
}
}
}
}
[Link] to find the index of the first occurrence of a substring in a string:
import [Link];
public class SubstringIndex {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the main string: ");
String mainString = [Link]();
[Link]("Enter the substring to find: ");
String subString = [Link]();
int index = findSubstringIndex(mainString, subString);
if (index != -1) {
[Link]("First occurrence of substring is at index: " + index);
} else {
[Link]("Substring not found in the main string.");
}
}
private static int findSubstringIndex(String mainString, String subString) {
for (int i = 0; i <= [Link]() - [Link](); i++) {
if ([Link](i, i + [Link]()).equals(subString)) {
return i;
}
}
return -1;
}
}
[Link] to find the longest common prefix among an array of strings:
import [Link];
public class LongestCommonPrefix {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of strings: ");
int n = [Link]();
[Link](); // Consume newline
String[] strings = new String[n];
for (int i = 0; i < n; i++) {
[Link]("Enter string " + (i + 1) + ": ");
strings[i] = [Link]();
}
String prefix = longestCommonPrefix(strings);
[Link]("Longest common prefix: " + prefix);
}
private static String longestCommonPrefix(String[] strings) {
if (strings == null || [Link] == 0) {
return "";
}
String prefix = strings[0];
for (int i = 1; i < [Link]; i++) {
int j = 0;
while (j < [Link]() && j < strings[i].length() && [Link](j) == strings[i].charAt(j))
{
j++;
}
prefix = [Link](0, j);
if ([Link]()) {
break;
}
}
return prefix;
}
}
[Link] to count the number of vowels and consonants in a string:
import [Link];
public class VowelConsonantCount {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]().toLowerCase();
int vowels = 0, consonants = 0;
for (char c : [Link]()) {
if (c >= 'a' && c <= 'z') {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
vowels++;
} else {
consonants++;
}
}
}
[Link]("Number of vowels: " + vowels);
[Link]("Number of consonants: " + consonants);
}
}
[Link] to reverse a sentence:
import [Link];
public class SentenceReversal {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a sentence: ");
String sentence = [Link]();
String reversed = reverseSentence(sentence);
[Link]("Reversed sentence: " + reversed);
}
private static String reverseSentence(String sentence) {
String[] words = [Link]().split("\\s+");
StringBuilder reversed = new StringBuilder();
for (int i = [Link] - 1; i >= 0; i--) {
[Link](words[i]).append(" ");
}
return [Link]().trim();
}
}
[Link] to find the index of the last occurrence of a character in a string:
import [Link];
public class LastIndexOfCharacter {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
[Link]("Enter a character to find its last index: ");
char target = [Link]().charAt(0);
int lastIndex = lastIndexOf(str, target);
if (lastIndex != -1) {
[Link]("Last index of '" + target + "': " + lastIndex);
} else {
[Link]("Character not found in the string.");
}
}
private static int lastIndexOf(String str, char target) {
for (int i = [Link]() - 1; i >= 0; i--) {
if ([Link](i) == target) {
return i;
}
}
return -1;
}
}
[Link] to remove all occurrences of a specified character from a string:
import [Link];
public class RemoveCharacter {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
[Link]("Enter a character to remove: ");
char target = [Link]().charAt(0);
String result = removeCharacter(str, target);
[Link]("String after removing '" + target + "': " + result);
}
private static String removeCharacter(String str, char target) {
StringBuilder result = new StringBuilder();
for (char c : [Link]()) {
if (c != target) {
[Link](c);
}
}
return [Link]();
}
}
[Link] to check if a string is a rotation of another string:
import [Link];
public class StringRotationCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
if (isRotation(str1, str2)) {
[Link]("The second string is a rotation of the first string.");
} else {
[Link]("The second string is not a rotation of the first string.");
}
}
private static boolean isRotation(String str1, String str2) {
if ([Link]() != [Link]()) {
return false;
}
String concatenated = str1 + str1;
return [Link](str2);
}
}
[Link] to find the frequency of each character in a string:
import [Link];
import [Link];
import [Link];
public class CharacterFrequency {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
Map<Character, Integer> frequencyMap = getCharacterFrequency(str);
[Link]("Character frequencies:");
for ([Link]<Character, Integer> entry : [Link]()) {
[Link]([Link]() + ": " + [Link]());
}
}
private static Map<Character, Integer> getCharacterFrequency(String str) {
Map<Character, Integer> frequencyMap = new HashMap<>();
for (char c : [Link]()) {
[Link](c, [Link](c, 0) + 1);
}
return frequencyMap;
}
}
[Link] to check if a string is a palindrome:
import [Link];
public class PalindromeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
if (isPalindrome(str)) {
[Link]("The string is a palindrome.");
} else {
[Link]("The string is not a palindrome.");
}
}
private static boolean isPalindrome(String str) {
int left = 0, right = [Link]() - 1;
while (left < right) {
if ([Link](left) != [Link](right)) {
return false;
}
left++;
right--;
}
return true;
}
}
[Link] to remove all spaces from a string:
import [Link];
public class RemoveSpaces {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String noSpaces = removeSpaces(str);
[Link]("String after removing spaces: " + noSpaces);
}
private static String removeSpaces(String str) {
StringBuilder result = new StringBuilder();
for (char c : [Link]()) {
if (c != ' ') {
[Link](c);
}
}
return [Link]();
}
}
[Link] to check if a string is an anagram of another string:
import [Link];
public class AnagramCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
if (areAnagrams(str1, str2)) {
[Link]("The strings are anagrams.");
} else {
[Link]("The strings are not anagrams.");
}
}
private static boolean areAnagrams(String str1, String str2) {
if ([Link]() != [Link]()) {
return false;
}
int[] count = new int[256];
for (char c : [Link]()) {
count[c]++;
}
for (char c : [Link]()) {
if (count[c] == 0) {
return false;
}
count[c]--;
}
return true;
}
}
[Link] to find the longest common substring between two strings:
import [Link];
public class LongestCommonSubstring {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
String longestCommonSubstring = findLongestCommonSubstring(str1, str2);
[Link]("Longest common substring: " + longestCommonSubstring);
}
private static String findLongestCommonSubstring(String str1, String str2) {
int m = [Link]();
int n = [Link]();
int[][] dp = new int[m + 1][n + 1];
int maxLength = 0, endIndex = 0;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if ([Link](i - 1) == [Link](j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
if (dp[i][j] > maxLength) {
maxLength = dp[i][j];
endIndex = i;
}
} else {
dp[i][j] = 0;
}
}
}
return [Link](endIndex - maxLength, endIndex);
}
}
[Link] to find all permutations of a string (distinct characters):
import [Link];
public class StringPermutations {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
[Link]("Permutations of the string:");
generatePermutations("", str);
}
private static void generatePermutations(String prefix, String remaining) {
if ([Link]()) {
[Link](prefix);
return;
}
for (int i = 0; i < [Link](); i++) {
char current = [Link](i);
String newPrefix = prefix + current;
String newRemaining = [Link](0, i) + [Link](i + 1);
generatePermutations(newPrefix, newRemaining);
}
}
}
[Link] to check if a string is a rotation of another string:
import [Link];
public class StringRotationCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
if (isRotation(str1, str2)) {
[Link]("The second string is a rotation of the first string.");
} else {
[Link]("The second string is not a rotation of the first string.");
}
}
private static boolean isRotation(String str1, String str2) {
if ([Link]() != [Link]()) {
return false;
}
String concatenated = str1 + str1;
return [Link](str2);
}
}
[Link] to reverse a string word by word:
import [Link];
public class ReverseWordsInString {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String reversed = reverseWords(str);
[Link]("String with reversed words: " + reversed);
}
private static String reverseWords(String str) {
String[] words = [Link]("\\s+");
StringBuilder reversed = new StringBuilder();
for (int i = [Link] - 1; i >= 0; i--) {
[Link](words[i]).append(" ");
}
return [Link]().trim();
}
}
[Link] to remove duplicates from a string:
import [Link];
public class RemoveDuplicatesFromString {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String withoutDuplicates = removeDuplicates(str);
[Link]("String after removing duplicates: " + withoutDuplicates);
}
private static String removeDuplicates(String str) {
StringBuilder result = new StringBuilder();
boolean[] visited = new boolean[256]; // Assuming ASCII characters
for (char c : [Link]()) {
if (!visited[c]) {
[Link](c);
visited[c] = true;
}
}
return [Link]();
}
}
[Link] to count the occurrences of each word in a string:
import [Link];
import [Link];
import [Link];
public class WordFrequency {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
Map<String, Integer> frequencyMap = getWordFrequency(str);
[Link]("Word frequencies:");
for ([Link]<String, Integer> entry : [Link]()) {
[Link]([Link]() + ": " + [Link]());
}
}
private static Map<String, Integer> getWordFrequency(String str) {
Map<String, Integer> frequencyMap = new HashMap<>();
String[] words = [Link]("\\s+");
for (String word : words) {
[Link](word, [Link](word, 0) + 1);
}
return frequencyMap;
}
}
[Link] to check if a string contains only digits:
import [Link];
public class ContainsOnlyDigits {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
if (containsOnlyDigits(str)) {
[Link]("The string contains only digits.");
} else {
[Link]("The string does not contain only digits.");
}
}
private static boolean containsOnlyDigits(String str) {
for (char c : [Link]()) {
if () {
return false;
}
}
return true;
}
}
[Link] to convert a string to title case:
import [Link];
public class TitleCaseConversion {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String titleCase = convertToTitleCase(str);
[Link]("Title case string: " + titleCase);
}
private static String convertToTitleCase(String str) {
StringBuilder result = new StringBuilder();
boolean capitalize = true;
for (char c : [Link]()) {
if ([Link](c)) {
if (capitalize) {
[Link]([Link](c));
capitalize = false;
} else {
[Link]([Link](c));
}
} else {
[Link](c);
capitalize = true;
}
}
return [Link]();
}
}
[Link] to check if a string is a valid shuffle of two other strings:
import [Link];
public class ValidShuffleCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
[Link]("Enter the shuffled string: ");
String shuffled = [Link]();
if (isValidShuffle(str1, str2, shuffled)) {
[Link]("The shuffled string is valid.");
} else {
[Link]("The shuffled string is not valid.");
}
}
private static boolean isValidShuffle(String str1, String str2, String shuffled) {
if ([Link]() + [Link]() != [Link]()) {
return false;
}
int i = 0, j = 0, k = 0;
while (k < [Link]()) {
if (i < [Link]() && [Link](i) == [Link](k)) {
i++;
} else if (j < [Link]() && [Link](j) == [Link](k)) {
j++;
} else {
return false;
}
k++;
}
return true;
}
}
[Link] to find the first non-repeating character in a string:
import [Link];
import [Link];
import [Link];
public class FirstNonRepeatingCharacter {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
char firstNonRepeating = findFirstNonRepeatingCharacter(str);
if (firstNonRepeating != '\0') {
[Link]("First non-repeating character: " + firstNonRepeating);
} else {
[Link]("No non-repeating characters found.");
}
}
private static char findFirstNonRepeatingCharacter(String str) {
Map<Character, Integer> frequencyMap = new HashMap<>();
for (char c : [Link]()) {
[Link](c, [Link](c, 0) + 1);
}
for (char c : [Link]()) {
if ([Link](c) == 1) {
return c;
}
}
return '\0';
}
}
[Link] to find the length of the last word in a string:
import [Link];
public class LengthOfLastWord {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]().trim();
int lengthOfLastWord = findLengthOfLastWord(str);
[Link]("Length of the last word: " + lengthOfLastWord);
}
private static int findLengthOfLastWord(String str) {
int length = 0;
for (int i = [Link]() - 1; i >= 0; i--) {
if ([Link](i) == ' ') {
if (length > 0) {
break;
}
} else {
length++;
}
}
return length;
}
}
[Link] to check if a string is a pangram (contains every letter of the alphabet at least
once):
import [Link];
public class PangramCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]().toLowerCase();
if (isPangram(str)) {
[Link]("The string is a pangram.");
} else {
[Link]("The string is not a pangram.");
}
}
private static boolean isPangram(String str) {
boolean[] visited = new boolean[26];
for (char c : [Link]()) {
if (c >= 'a' && c <= 'z') {
visited[c - 'a'] = true;
}
}
for (boolean value : visited) {
if (!value) {
return false;
}
}
return true;
}
}
[Link] to reverse a string without using extra space:
import [Link];
public class ReverseString {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
char[] charArray = [Link]();
reverse(charArray);
String reversed = new String(charArray);
[Link]("Reversed string: " + reversed);
}
private static void reverse(char[] charArray) {
int start = 0, end = [Link] - 1;
while (start < end) {
char temp = charArray[start];
charArray[start] = charArray[end];
charArray[end] = temp;
start++;
end--;
}
}
}
[Link] to find the smallest window in a string containing all characters of another string:
import [Link];
import [Link];
import [Link];
public class SmallestWindowContainingCharacters {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the main string: ");
String mainString = [Link]();
[Link]("Enter the target string: ");
String targetString = [Link]();
String smallestWindow = findSmallestWindow(mainString, targetString);
[Link]("Smallest window: " + smallestWindow);
}
private static String findSmallestWindow(String mainString, String targetString) {
Map<Character, Integer> targetMap = new HashMap<>();
for (char c : [Link]()) {
[Link](c, [Link](c, 0) + 1);
}
int count = 0;
int start = 0, minLength = Integer.MAX_VALUE;
String smallestWindow = "";
for (int end = 0; end < [Link](); end++) {
char current = [Link](end);
if ([Link](current)) {
[Link](current, [Link](current) - 1);
if ([Link](current) >= 0) {
count++;
}
}
while (count == [Link]()) {
if (end - start + 1 < minLength) {
minLength = end - start + 1;
smallestWindow = [Link](start, end + 1);
}
char left = [Link](start);
if ([Link](left)) {
[Link](left, [Link](left) + 1);
if ([Link](left) > 0) {
count--;
}
}
start++;
}
}
return smallestWindow;
}
}
[Link] to find the longest substring without repeating characters:
import [Link];
import [Link];
import [Link];
public class LongestSubstringWithoutRepeating {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
int length = longestSubstringWithoutRepeating(str);
[Link]("Length of the longest substring without repeating characters: " +
length);
}
private static int longestSubstringWithoutRepeating(String str) {
int maxLength = 0;
Set<Character> set = new HashSet<>();
int left = 0, right = 0;
while (right < [Link]()) {
char current = [Link](right);
if () {
[Link](current);
right++;
maxLength = [Link](maxLength, right - left);
} else {
[Link]([Link](left));
left++;
}
}
return maxLength;
}
}
[Link] to find the longest palindromic substring in a string:
import [Link];
public class LongestPalindromicSubstring {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String longestPalindrome = longestPalindromicSubstring(str);
[Link]("Longest palindromic substring: " + longestPalindrome);
}
private static String longestPalindromicSubstring(String str) {
if (str == null || [Link]() < 1) {
return "";
}
int start = 0, end = 0;
for (int i = 0; i < [Link](); i++) {
int len1 = expandAroundCenter(str, i, i);
int len2 = expandAroundCenter(str, i, i + 1);
int maxLength = [Link](len1, len2);
if (maxLength > end - start) {
start = i - (maxLength - 1) / 2;
end = i + maxLength / 2;
}
}
return [Link](start, end + 1);
}
private static int expandAroundCenter(String str, int left, int right) {
while (left >= 0 && right < [Link]() && [Link](left) == [Link](right)) {
left--;
right++;
}
return right - left - 1;
}
}
[Link] to find the maximum occurring character in a string:
import [Link];
public class MaximumOccurringCharacter {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
char maxOccurring = maximumOccurringCharacter(str);
[Link]("Maximum occurring character: " + maxOccurring);
}
private static char maximumOccurringCharacter(String str) {
int[] count = new int[256];
for (char c : [Link]()) {
count[c]++;
}
int maxCount = Integer.MIN_VALUE;
char maxChar = '\0';
for (int i = 0; i < 256; i++) {
if (count[i] > maxCount) {
maxCount = count[i];
maxChar = (char) i;
}
}
return maxChar;
}
}
[Link] to find the first recurring character in a string:
import [Link];
import [Link];
import [Link];
public class FirstRecurringCharacter {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
char firstRecurring = findFirstRecurringCharacter(str);
if (firstRecurring != '\0') {
[Link]("First recurring character: " + firstRecurring);
} else {
[Link]("No recurring character found.");
}
}
private static char findFirstRecurringCharacter(String str) {
Set<Character> set = new HashSet<>();
for (char c : [Link]()) {
if ([Link](c)) {
return c;
}
[Link](c);
}
return '\0';
}
}
[Link] to find all permutations of a string (with duplicate characters):
import [Link];
import [Link];
import [Link];
public class PermutationsWithDuplicates {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
List<String> permutations = generatePermutations(str);
[Link]("Permutations of the string:");
for (String permutation : permutations) {
[Link](permutation);
}
}
private static List<String> generatePermutations(String str) {
List<String> permutations = new ArrayList<>();
generatePermutationsHelper([Link](), 0, permutations);
return permutations;
}
private static void generatePermutationsHelper(char[] chars, int index, List<String>
permutations) {
if (index == [Link] - 1) {
[Link](new String(chars));
return;
}
for (int i = index; i < [Link]; i++) {
if (shouldSwap(chars, index, i)) {
swap(chars, index, i);
generatePermutationsHelper(chars, index + 1, permutations);
swap(chars, index, i);
}
}
}
private static boolean shouldSwap(char[] chars, int start, int end) {
for (int i = start; i < end; i++) {
if (chars[i] == chars[end]) {
return false;
}
}
return true;
}
private static void swap(char[] chars, int i, int j) {
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
}
[Link] to check if two strings are anagrams (with duplicate characters):
import [Link];
import [Link];
import [Link];
public class AnagramsWithDuplicates {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
if (areAnagrams(str1, str2)) {
[Link]("The strings are anagrams.");
} else {
[Link]("The strings are not anagrams.");
}
}
private static boolean areAnagrams(String str1, String str2) {
if ([Link]() != [Link]()) {
return false;
}
Map<Character, Integer> frequencyMap = new HashMap<>();
for (char c : [Link]()) {
[Link](c, [Link](c, 0) + 1);
}
for (char c : [Link]()) {
if ( || [Link](c) == 0) {
return false;
}
[Link](c, [Link](c) - 1);
}
return true;
}
}
[Link] to find the longest common prefix among an array of strings:
import [Link];
public class LongestCommonPrefix {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of strings: ");
int n = [Link]();
[Link](); // Consume newline
String[] strings = new String[n];
[Link]("Enter the strings:");
for (int i = 0; i < n; i++) {
strings[i] = [Link]();
}
String longestCommonPrefix = findLongestCommonPrefix(strings);
[Link]("Longest common prefix: " + longestCommonPrefix);
}
private static String findLongestCommonPrefix(String[] strings) {
if (strings == null || [Link] == 0) {
return "";
}
String prefix = strings[0];
for (int i = 1; i < [Link]; i++) {
while (strings[i].indexOf(prefix) != 0) {
prefix = [Link](0, [Link]() - 1);
if ([Link]()) {
return "";
}
}
}
return prefix;
}
}
[Link] to count the number of vowels and consonants in a string:
import [Link];
public class VowelConsonantCount {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]().toLowerCase();
int vowels = 0, consonants = 0;
for (char c : [Link]()) {
if (c >= 'a' && c <= 'z') {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
vowels++;
} else {
consonants++;
}
}
}
[Link]("Number of vowels: " + vowels);
[Link]("Number of consonants: " + consonants);
}
}
[Link] to find the longest substring without repeating characters:
import [Link];
import [Link];
import [Link];
public class LongestSubstringWithoutRepeating {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
int maxLength = longestSubstringWithoutRepeating(str);
[Link]("Length of the longest substring without repeating characters: " +
maxLength);
}
private static int longestSubstringWithoutRepeating(String str) {
int maxLength = 0;
Set<Character> set = new HashSet<>();
int left = 0, right = 0;
while (right < [Link]()) {
char current = [Link](right);
if () {
[Link](current);
right++;
maxLength = [Link](maxLength, right - left);
} else {
[Link]([Link](left));
left++;
}
}
return maxLength;
}
}
[Link] to find the second most frequent character in a string:
import [Link];
import [Link];
import [Link];
public class SecondMostFrequentCharacter {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
char secondMostFrequent = findSecondMostFrequentCharacter(str);
[Link]("Second most frequent character: " + secondMostFrequent);
}
private static char findSecondMostFrequentCharacter(String str) {
Map<Character, Integer> frequencyMap = new HashMap<>();
for (char c : [Link]()) {
[Link](c, [Link](c, 0) + 1);
}
int maxFrequency = Integer.MIN_VALUE;
int secondMaxFrequency = Integer.MIN_VALUE;
char secondMostFrequentChar = '\0';
for ([Link]<Character, Integer> entry : [Link]()) {
int frequency = [Link]();
if (frequency > maxFrequency) {
secondMaxFrequency = maxFrequency;
maxFrequency = frequency;
secondMostFrequentChar = [Link]();
} else if (frequency > secondMaxFrequency && frequency < maxFrequency) {
secondMaxFrequency = frequency;
secondMostFrequentChar = [Link]();
}
}
return secondMostFrequentChar;
}
}
[Link] to check if a string is a valid shuffle of two other strings (with duplicate characters):
import [Link];
import [Link];
import [Link];
public class ValidShuffleWithDuplicates {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
[Link]("Enter the shuffled string: ");
String shuffled = [Link]();
if (isValidShuffle(str1, str2, shuffled)) {
[Link]("The shuffled string is valid.");
} else {
[Link]("The shuffled string is not valid.");
}
}
private static boolean isValidShuffle(String str1, String str2, String shuffled) {
if ([Link]() + [Link]() != [Link]()) {
return false;
}
Map<Character, Integer> frequencyMap = new HashMap<>();
for (char c : [Link]()) {
[Link](c, [Link](c, 0) + 1);
}
for (char c : [Link]()) {
[Link](c, [Link](c, 0) + 1);
}
for (char c : [Link]()) {
if ( || [Link](c) == 0) {
return false;
}
[Link](c, [Link](c) - 1);
}
return true;
}
}
[Link] to check if a string is a palindrome (ignoring non-alphanumeric characters):
import [Link];
public class PalindromeCheckIgnoringNonAlphaNumeric {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]().toLowerCase();
if (isPalindromeIgnoringNonAlphaNumeric(str)) {
[Link]("The string is a palindrome.");
} else {
[Link]("The string is not a palindrome.");
}
}
private static boolean isPalindromeIgnoringNonAlphaNumeric(String str) {
int left = 0, right = [Link]() - 1;
while (left < right) {
char leftChar = [Link](left);
char rightChar = [Link](right);
if () {
left++;
} else if () {
right--;
} else if (leftChar != rightChar) {
return false;
} else {
left++;
right--;
}
}
return true;
}
}
[Link] to check if a string is an interleaving of two other strings:
import [Link];
public class InterleavingCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String s1 = [Link]();
[Link]("Enter the second string: ");
String s2 = [Link]();
[Link]("Enter the interleaved string: ");
String interleaved = [Link]();
if (isInterleaved(s1, s2, interleaved)) {
[Link]("The string is an interleaving of the two strings.");
} else {
[Link]("The string is not an interleaving of the two strings.");
}
}
private static boolean isInterleaved(String s1, String s2, String interleaved) {
int m = [Link](), n = [Link]();
if (m + n != [Link]()) {
return false;
}
boolean[][] dp = new boolean[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 && j == 0) {
dp[i][j] = true;
} else if (i == 0) {
dp[i][j] = dp[i][j - 1] && [Link](j - 1) == [Link](i + j - 1);
} else if (j == 0) {
dp[i][j] = dp[i - 1][j] && [Link](i - 1) == [Link](i + j - 1);
} else {
dp[i][j] = (dp[i - 1][j] && [Link](i - 1) == [Link](i + j - 1)) ||
(dp[i][j - 1] && [Link](j - 1) == [Link](i + j - 1));
}
}
}
return dp[m][n];
}
}
[Link] to remove duplicate characters from a string:
import [Link];
public class RemoveDuplicates {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String result = removeDuplicates(str);
[Link]("String after removing duplicates: " + result);
}
private static String removeDuplicates(String str) {
StringBuilder result = new StringBuilder();
boolean[] visited = new boolean[256];
for (char c : [Link]()) {
if (!visited[c]) {
[Link](c);
visited[c] = true;
}
}
return [Link]();
}
}
[Link] to find the longest palindromic substring in a string:
import [Link];
public class LongestPalindromicSubstring {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
String longestPalindrome = findLongestPalindrome(str);
[Link]("Longest palindromic substring: " + longestPalindrome);
}
private static String findLongestPalindrome(String str) {
int maxLength = 0, start = 0, end = 0;
for (int i = 0; i < [Link](); i++) {
int len1 = expandAroundCenter(str, i, i);
int len2 = expandAroundCenter(str, i, i + 1);
int length = [Link](len1, len2);
if (length > maxLength) {
maxLength = length;
start = i - (length - 1) / 2;
end = i + length / 2;
}
}
return [Link](start, end + 1);
}
private static int expandAroundCenter(String str, int left, int right) {
while (left >= 0 && right < [Link]() && [Link](left) == [Link](right)) {
left--;
right++;
}
return right - left - 1;
}
}
[Link] to find the frequency of each character in a string:
import [Link];
import [Link];
import [Link];
public class CharacterFrequency {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
Map<Character, Integer> frequencyMap = new HashMap<>();
for (char c : [Link]()) {
if ([Link](c)) {
[Link](c, [Link](c, 0) + 1);
}
}
[Link]("Character frequencies:");
for ([Link]<Character, Integer> entry : [Link]()) {
[Link]([Link]() + ": " + [Link]());
}
}
}
[Link] to find all possible substrings of a string:
import [Link];
public class AllSubstrings {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
[Link]("All substrings of the string:");
for (int i = 0; i < [Link](); i++) {
for (int j = i + 1; j <= [Link](); j++) {
[Link]([Link](i, j));
}
}
}
}
[Link] to check if a string is a rotation of another string:
import [Link];
public class RotationCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
if (isRotation(str1, str2)) {
[Link]("The second string is a rotation of the first string.");
} else {
[Link]("The second string is not a rotation of the first string.");
}
}
private static boolean isRotation(String str1, String str2) {
if ([Link]() != [Link]()) {
return false;
}
String concatenated = str1 + str1;
return [Link](str2);
}
}
[Link] to find the longest substring with at most K distinct characters:
import [Link];
import [Link];
import [Link];
public class LongestSubstringWithKDistinct {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
[Link]("Enter the maximum number of distinct characters (K): ");
int k = [Link]();
int maxLength = longestSubstringWithKDistinct(str, k);
[Link]("Length of the longest substring with at most " + k + " distinct
characters: " + maxLength);
}
private static int longestSubstringWithKDistinct(String str, int k) {
int maxLength = 0;
Map<Character, Integer> map = new HashMap<>();
int left = 0, right = 0;
while (right < [Link]()) {
char current = [Link](right);
[Link](current, [Link](current, 0) + 1);
while ([Link]() > k) {
char leftChar = [Link](left);
[Link](leftChar, [Link](leftChar) - 1);
if ([Link](leftChar) == 0) {
[Link](leftChar);
}
left++;
}
maxLength = [Link](maxLength, right - left + 1);
right++;
}
return maxLength;
}
}