String Problems
Reverse the string
public static String reverseString(String s) {
if (s == null) return null;
char[] chars = s.toCharArray();
int l = 0, r = chars.length - 1;
while (l < r) {
char tmp = chars[l];
chars[l] = chars[r];
chars[r] = tmp;
l++;
r--;
}
return new String(chars);
}
Input: "hello"
Output: "olleh"
Check Palindrome
public static boolean isPalindrome(String s) {
if (s == null) return false;
int l = 0, r = s.length() - 1;
while (l < r) {
if (s.charAt(l) != s.charAt(r)) return false;
l++;
r--;
}
return true;
}
Input: "madam"
Output: true
First Non-Repeating Character
public static char firstUniqueCharacter(String s) {
if (s == null || s.isEmpty()) return '-';
int[] freq = new int[256];
for (char c : s.toCharArray()) freq[c]++;
for (char c : s.toCharArray()) {
if (freq[c] == 1) return c;
}
return '-';
}
Input: "swiss"
Output: 'w'
Count Vowels and Consonants
public static void countVowelsAndConsonants(String s) {
if (s == null) {
System.out.println("Null string");
return;
}
int vowels = 0, consonants = 0;
for (char c : s.toLowerCase().toCharArray()) {
if ("aeiou".indexOf(c) != -1) vowels++;
else if (c >= 'a' && c <= 'z') consonants++;
}
System.out.println("Vowels = " + vowels + ", Consonants = " + consonants);
}
Input: "hello"
Output: Vowels = 2, Consonants = 3
Check Anagram
public static boolean isAnagram(String s1, String s2) {
if (s1 == null || s2 == null || s1.length() != s2.length()) return false;
int[] freq = new int[256];
for (char c : s1.toCharArray()) freq[c]++;
for (char c : s2.toCharArray()) {
if (--freq[c] < 0) return false;
}
return true;
}
Input: "listen", "silent"
Output: true
Longest Substring Without Repeating Characters
public static void longestSubstringWithoutRepeatingCharacters(String s) {
if (s == null || s.isEmpty()) {
System.out.println("Empty string");
return;
}
Map map = new HashMap<>();
int left = 0, maxLen = 0, start = 0;
for (int right = 0; right < s.length(); right++) {
if (map.containsKey(s.charAt(right))) {
left = Math.max(left, map.get(s.charAt(right)) + 1);
}
map.put(s.charAt(right), right);
if (right - left + 1 > maxLen) {
maxLen = right - left + 1;
start = left;
}
}
String longestSubstr = s.substring(start, start + maxLen);
System.out.println("Longest Substring: " + longestSubstr + " (Length: " + maxLen + ")");
}
Input: "abcabcbb"
Output: "abc" (Length: 3)
Remove Duplicates
public static void removeDuplicates(String str) {
if (str == null) return;
Set seen = new LinkedHashSet<>();
for (char c : str.toCharArray()) seen.add(c);
StringBuilder sb = new StringBuilder();
for (char c : seen) sb.append(c);
System.out.println("After removing duplicates: " + sb);
}
Input: "programming"
Output: "progamin"
String Compression
public static void compressString(String s) {
if (s == null || s.isEmpty()) return;
int count = 1;
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= s.length(); i++) {
if (i < s.length() && s.charAt(i) == s.charAt(i - 1)) {
count++;
} else {
sb.append(s.charAt(i - 1)).append(count);
count = 1;
}
}
System.out.println(s + " after compression = " + sb);
}
Input: "aabcccccaaa"
Output: "a2b1c5a3"
Check Isomorphic Strings
public static void checkIsomorphicStrings(String s, String t) {
if (s == null || t == null || s.length() != t.length()) {
System.out.println("Not isomorphic");
return;
}
int[] mapS = new int[256];
int[] mapT = new int[256];
for (int i = 0; i < s.length(); i++) {
if (mapS[s.charAt(i)] != mapT[t.charAt(i)]) {
System.out.println("Not isomorphic");
return;
}
mapS[s.charAt(i)] = i + 1;
mapT[t.charAt(i)] = i + 1;
}
System.out.println("Isomorphic");
}
Input: "egg", "add"
Output: "Isomorphic"
All Permutations
public static void printAllPermutations(String s, String ans) {
if (s == null) return;
if (s.length() == 0) {
System.out.println(ans);
return;
}
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
String rem = s.substring(0, i) + s.substring(i + 1);
printAllPermutations(rem, ans + c);
}
}
Input: "abc"
Output: "abc, acb, bac, bca, cab, cba"
Count Words
public static void countWordInString(String s) {
if (s == null) {
System.out.println("0");
return;
}
s = s.trim();
if (s.isEmpty()) {
System.out.println("0");
return;
}
int count = s.split("\\s+").length;
System.out.println("Words = " + count);
}
Input: "Hello World from Java"
Output: 4
Duplicate Characters
public static void duplicateCharactersInString(String s) {
if (s == null) return;
int[] freq = new int[256];
for (char c : s.toCharArray()) freq[c]++;
for (int i = 0; i < 256; i++) {
if (freq[i] > 1) {
System.out.println((char) i + " -> " + freq[i]);
}
}
}
Input: "programming"
Output: g -> 2, r -> 2, m -> 2
All Substrings
public static void findAllSubString(String str) {
if (str == null) return;
for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j <= str.length(); j++) {
System.out.println(str.substring(i, j));
}
}
}
Input: "abc"
Output: a, ab, abc, b, bc, c
Sort String
public static void sortString(String str) {
if (str == null) return;
char[] chars = str.toCharArray();
Arrays.sort(chars);
System.out.println(new String(chars));
}
Input: "dcba"
Output: "abcd"
Longest Common Prefix
public static void longestPrefix(String[] words) {
if (words == null || words.length == 0) {
System.out.println("Empty string array");
return;
}
String prefix = words[0];
for (int i = 1; i < words.length; i++) {
while (!words[i].startsWith(prefix)) {
prefix = prefix.substring(0, prefix.length() - 1);
if (prefix.isEmpty()) {
System.out.println("No matching prefix");
return;
}
}
}
System.out.println("Prefix = " + prefix);
}
Input: {"flower","flow","flight"}
Output: "fl"