String
String
import java.util.*;
class HelloWorld {
static int function(String str){
HashSet<Character> l1=new HashSet<>();
int totalcount=0;
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
if(l1.contains(ch)){
totalcount+=1;
}else{
totalcount+=2;
l1.add(ch);
}
}
return totalcount;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
int ans=function(str);
System.out.println(ans);
}
}
3. Input:Hello World output: World Hello
import java.util.*;
class HelloWorld {
static String function(String str){
String[] words=str.split(" ");
String rev=" ";
for(int i=words.length-1;i>=0;i--){
rev+=words[i];
rev+=" ";
}
return rev.trim();
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
String ans=function(str);
System.out.println(ans);
}
}
4. Return the string after removing all vowels in the string
import java.util.*;
public class Main{
static String function(String str){
StringBuilder sb = new StringBuilder();
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
int flag=1;
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'
||ch=='u'||ch=='a'||ch=='e'||ch=='i'||
ch=='o'||ch=='u'){
flag=0;
}
if(flag==1){
sb.append(ch);
}
}
return sb.toString();
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
String ans=function(str);
System.out.println(ans);
}
}
//or,
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
String ans = str.replaceAll("[AEIOUaeiou]", "");
System.out.println(ans);
}
}
import java.util.*;
public class Main
{
static boolean function(String str1,String str2){
StringBuilder sb = new StringBuilder();
/*if ans is not case sensetive
str1=str1.toUpperCase();
str2=str2.toUpperCase(); */
if(str1.length()!=str2.length()){
return false;
}
char[] chararr1=str1.toCharArray();
char[] chararr2=str2.toCharArray();
Arrays.sort(chararr1);
Arrays.sort(chararr2);
boolean answer=Arrays.equals(chararr1,chararr2);
return answer;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String str1=sc.nextLine();
System.out.println("Enter another String");
String str2=sc.nextLine();
boolean ans=function(str1,str2);
System.out.println(ans);
}
}
6. You have been given two strings A and B. You have to find the difference in the
count of whitespaces in both the strings. Then you return a string even or odd
along with the difference
Input 1:
He ll o Wo rl d
Hello World
Output 1:
Even:4
import java.util.*;
public class Main
{
static String function(String str1,String str2){
StringBuilder sb = new StringBuilder();
int count1 = 0;
for (int i = 0; i < str1.length(); i++) {
if (str1.charAt(i) == ' ') {
count1++;
}
}
int count2 = 0;
for (int i = 0; i < str2.length(); i++) {
if (str2.charAt(i) == ' ') {
count2++;
}
}
int diff=Math.abs(count1-count2);
if(diff%2==0){
sb.append("Even");
}else{
sb.append("Odd");
}
sb.append(':');
sb.append(diff);
return sb.toString();
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a String");
String str1=sc.nextLine();
System.out.println("Enter another String");
String str2=sc.nextLine();
String ans=function(str1,str2);
System.out.println(ans);
}
}
7. Sum of Ascii value of the characters in a string
Input: Hello World
Output: 1052
import java.util.*;
public class Main{
static int function(String str){
int sum=0;
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
int c=ch;
sum=sum+c;
}
return sum;
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
int ans=function(str);
System.out.println(ans);
}
}
8. Find out longest Substring and its length without repeating
characters.
Input: abcabcbb
Output:abc,3
//all the substring without repeating characters are
abc,bca,cab,abc,bc,cb,b,b
import java.util.*;
public class A{
static void function(String str){
String longestTillnow="";
String longestoverall="";
ArrayList<Character> l1=new ArrayList<>();
for(int i=0;i<str.length();i++){
for(int j=i;j<str.length();j++){
char ch=str.charAt(j);
if(l1.contains(ch)){
longestTillnow="";
l1.clear();
break;
}
l1.add(ch);
longestTillnow+=ch;
if(longestTillnow.length()>longestoverall.length()){
longestoverall=longestTillnow;
}
}
}
System.out.println("longest substring="+longestoverall);
System.out.println("length="+ longestoverall.length());
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
function(str);
}
}
9. Input: aaabbbbccddrr output: a3b4c2d2r2
import java.util.*;
public class A{
static String function(String str){
StringBuilder sb=new StringBuilder();
HashMap<Character,Integer> m1=new LinkedHashMap<>();
int val=1;
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
if(m1.containsKey(ch)){
m1.put(ch,m1.get(ch)+1);
}else{
m1.put(ch,val);
}
}
for(char key:m1.keySet()){
sb.append(key);
sb.append(m1.get(key));
}
return sb.toString();
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
String ans=function(str);
System.out.println(ans);
}
}
10. 4
ADAOAS [Explanation: 0,2,4 index same character so, output 1 ]
1
MADAM [0,2 index diff character]
0
ADAOPS [0,2 index same character but 0,4 index diff character]
0
aba1a0*
0
import java.util.*;
public class A{
static int function(String str){
HashSet<Character> s1= new HashSet<>();
s1.add(str.charAt(0));
for(int i=0;i<str.length();i+=2){
char ch=str.charAt(i);
if(!s1.contains(ch)){
return 0;
}
}
return 1;
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine(); //to consume leftover newline
for(int i=0;i<n;i++){// for multiple testcases
String str=sc.nextLine();
int ans=function(str);
System.out.println(ans);
}
}
}
//nextInt() only reads the integer value and not the newline character \
n that occurs when you press Enter. So, when nextLine() is called right
after nextInt(), it reads this leftover newline instead of waiting for user
input.