0% found this document useful (0 votes)
6 views3 pages

Strings 10 Questions With Java Code

The document provides ten Java interview questions related to string manipulation, each accompanied by a code solution. Topics include reversing a string, checking for palindromes, counting vowels and consonants, and validating parentheses. The solutions demonstrate various algorithms and data structures, such as arrays, hash sets, and stacks.

Uploaded by

immortalxxx09
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Strings 10 Questions With Java Code

The document provides ten Java interview questions related to string manipulation, each accompanied by a code solution. Topics include reversing a string, checking for palindromes, counting vowels and consonants, and validating parentheses. The solutions demonstrate various algorithms and data structures, such as arrays, hash sets, and stacks.

Uploaded by

immortalxxx09
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

STRINGS – 10 INTERVIEW QUESTIONS WITH JAVA CODE

1. Reverse a string
static String reverseString(String s){
char[] arr = [Link]();
int l = 0, r = [Link] - 1;
while(l < r){
char temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
l++; r--;
}
return new String(arr);
}

2. Check if string is palindrome


static boolean isPalindrome(String s){
int l = 0, r = [Link]() - 1;
while(l < r){
if([Link](l++) != [Link](r--))
return false;
}
return true;
}

3. Check if two strings are anagrams


static boolean areAnagrams(String a, String b){
if([Link]() != [Link]()) return false;
int[] count = new int[26];
for(char c : [Link]()) count[c - 'a']++;
for(char c : [Link]()) count[c - 'a']--;
for(int x : count) if(x != 0) return false;
return true;
}

4. Count vowels and consonants


static void countVowelsConsonants(String s){
int vowels = 0, consonants = 0;
s = [Link]();
for(char c : [Link]()){
if(c >= 'a' && c <= 'z'){
if("aeiou".indexOf(c) != -1)
vowels++;
else
consonants++;
}
}
[Link]("Vowels = " + vowels + ", Consonants = " + consonants);
}

5. First non-repeating character


static char firstNonRepeating(String s){
int[] freq = new int[256];
for(char c : [Link]()) freq[c]++;
for(char c : [Link]())
if(freq[c] == 1)
return c;
return '\0';
}
6. Longest substring without repeating characters
static int longestSubstring(String s){
HashSet<Character> set = new HashSet<>();
int l = 0, max = 0;
for(int r = 0; r < [Link](); r++){
while([Link]([Link](r)))
[Link]([Link](l++));
[Link]([Link](r));
max = [Link](max, r - l + 1);
}
return max;
}

7. Remove duplicate characters


static String removeDuplicates(String s){
HashSet<Character> set = new HashSet<>();
StringBuilder sb = new StringBuilder();
for(char c : [Link]()){
if([Link](c))
[Link](c);
}
return [Link]();
}

8. String compression
static String compressString(String s){
StringBuilder sb = new StringBuilder();
int count = 1;
for(int i = 1; i <= [Link](); i++){
if(i < [Link]() && [Link](i) == [Link](i - 1))
count++;
else{
[Link]([Link](i - 1)).append(count);
count = 1;
}
}
return [Link]();
}

9. Valid parentheses
static boolean isValidParentheses(String s){
Stack<Character> st = new Stack<>();
for(char c : [Link]()){
if(c == '(') [Link](')');
else if(c == '{') [Link]('}');
else if(c == '[') [Link](']');
else if([Link]() || [Link]() != c)
return false;
}
return [Link]();
}

10. Longest common prefix


static String longestCommonPrefix(String[] strs){
if([Link] == 0) return "";
String prefix = strs[0];
for(int i = 1; i < [Link]; i++){
while(!strs[i].startsWith(prefix)){
prefix = [Link](0, [Link]() - 1);
if([Link]()) return "";
}
}
return prefix;
}

You might also like