Java String Programming Problems: Preparation for Interviews and Applications
Meta Description (155 characters):
Learn how to solve the 11 most popular programming problems with the Java String class (Palindrome, Inversion, Immutability, Character Count) with modern Java features.
🎯 What Will You Learn in This Guide?
This guide contains solutions to the most frequently asked algorithmic and interview questions with the Java String class.
Aim; character counting, palindrome checking, String inversion, immutability and similar operations are to help you learn modern Java 8+ techniques.
All examples are described with Stream API, Lambda expressions and StringBuilder structures.
✍️ Basic String Programming Solutions
1️⃣ Finding Repeated Characters and Their Numbers
The HashMap and Map.merge() methods are used to find the number of times each character occurs in a text.
for (char c : input.toCharArray())
charsWithCountMap.merge(c, 1, Integer::sum);
System.out.println(charsWithCountMap);
➡️ Description: Map.merge() takes the character as a key and increments its counter.
Alternative – via Stream API:
List<Character> list = input.chars().mapToObj(c -> (char) c).collect(Collectors.toList());
list.forEach(c -> charsWithCountMap.merge(c, 1, Integer::sum));
System.out.println(charsWithCountMap);
2️⃣ Inverting a String
The safest method is the reverse() method of the StringBuilder class.
private static void reverseInputString(String input) {
StringBuilder sb = new StringBuilder(input);
String result = sb.reverse().toString();
System.out.println(result);
}
➡️ Description: StringBuilder is performant because it is mutable.
3️⃣ Checking for Palindromes
If the text reads the same backwards, it is a palindrome.
private static void checkPalindromeString(String input) {
boolean result = true;
int length = input.length();
for (int i = 0; i < length / 2; i++) {
if (input.charAt(i) != input.charAt(length - i - 1)) {
result = false;
break;
}
}
System.out.println(input + " bir palindromdur = " + result);
}
➡️ Description: The first and last characters are compared sequentially; If there is a difference, the loop ends.
4️⃣ Removing a Specific Character
The replaceAll() method is used to remove the desired characters from the text.
private static void removeCharFromString(String input, char c) {
String result = input.replaceAll(String.valueOf(c), "");
System.out.println(result);
}
➡️ Description: replaceAll() removes the character by replacing it with the empty string.
5️⃣ Proving the Immutability of String
String objects in Java are immutable. When a new value is assigned, a new object is created.
String s1 = "Java";
String s2 = s1;
s1 = "Python";
System.out.println(s2); // Java
➡️ Description: When s1 changes, a new “Python” object is created, while s2 still points to “Java”.
6️⃣ Finding the Number of Words in a Text
Returns the correct word count even if there are spaces, tabs, or extra spacing.
private static void countNumberOfWords(String line) {
String trimmedLine = line.trim();
int count = trimmedLine.isEmpty() ? 0 : trimmedLine.split("\\s+").length;
System.out.println(count);
}
➡️ Description: split("\s+") counts multiple spaces as one space.
🛠️ Advanced String Programming Solutions
7️⃣ Checking Whether Two Strings Contain the Same Characters
Whether the character sets of two Strings are the same is checked with the Set structure.
private static void sameCharsStrings(String s1, String s2) {
Set<Character> set1 = s1.chars().mapToObj(c -> (char) c).collect(Collectors.toSet());
Set<Character> set2 = s2.chars().mapToObj(c -> (char) c).collect(Collectors.toSet());
System.out.println(set1.equals(set2));
}
➡️ Description: Set.equals() checks whether two sets have exactly the same elements.
8️⃣ Checking If a String Contains Another String
private static boolean stringContainsSubstring(String string, String substring) {
return string.contains(substring);
}
➡️ Description: contains() method is used to search for substring.
9️⃣ Swapping Strings Without Using a Third Variable
s1 = s1.concat(s2);
s2 = s1.substring(0, s1.length() - s2.length());
s1 = s1.substring(s2.length());
➡️ Description: The substring() method temporarily swaps the values of two Strings.
🔎 1️⃣0️⃣ Finding the First Non-Repeating Character
private static Character printFirstNonRepeatingChar(String string) {
char[] chars = string.toCharArray();
List<Character> discarded = new ArrayList<>();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (discarded.contains(c)) continue;
boolean repeated = false;
for (int j = i + 1; j < chars.length; j++) {
if (c == chars[j]) {
repeated = true;
discarded.add(c);
break;
}
}
if (!repeated) return c;
}
return null;
}
➡️ Description: The first non-repeating character is returned.
1️⃣1️⃣ Checking If String Contains Only Numbers
🔹 Method 1: Regex (matches)
if (string.matches("\\d+"))
System.out.println("Sadece Rakam İçerir: " + string);
🔹 Method 2: Type Conversion (parseLong)
try {
Long.parseLong(string);
System.out.println("Sadece Rakam İçerir: " + string);
} catch (Exception e) {
System.out.println("Rakam Dışı Karakter Var: " + string);
}
➡️ Description: Regex method provides character-based control; parseLong tests numerical validity.
❓ Frequently Asked Questions (FAQ)
- Why do we use StringBuilder?
Since String is immutable, a new object is created with each operation. StringBuilder provides performance because it is modifiable.
- If the string does not change, how is it "changed"?
What changes is not the object, but the reference. When you make a new assignment, a new object is created.
- What is the advantage of using Stream API?
Less code provides more readability and functional structure.
- Why is only half of the sequence checked in palindrome checking?
Due to its symmetrical structure, the first half is the opposite of the second half. It is not necessary to check everything.
- What is the difference between replaceAll() and replace()?
replaceAll supports regex; replace just does direct character matching.
🏁 Conclusion
With this guide, you learned 11 basic problems in Java String manipulation. By improving your coding practices, you can specialize in String operations both in interviews and in real projects.
💡 Speed up your learning process by trying your applications on the GenixNode Developer Platform. 🚀

