Strings
string related problems with sample outputs are given below
string related problems with sample outputs are given below
1) string left rotation by K index
import java.util.*;
public class Main
{
static String leftrotate(String str, int d)
{
String ans = str.substring(d) + str.substring(0, d);
return ans;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str1 = sc.next();
int k=sc.nextInt();
System.out.println(leftrotate(str1, k));
}
}
input:
codeJamm 2
output:
deJammCo
2) string right rotation by k index
import java.util.*;
public class Main
{
static String rightrotate(String str, int d)
{
int l=str.length();
String ans=str.substring(l-d,l)+str.substring(0,l-d);
return ans;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str1 = sc.next();
int k=sc.nextInt();
System.out.println(leftrotate(str1, k));
}
}
input:
CodeJamm 3
output:
ammcodeJ
3) sort K length substrings
import java.util.*;
public class Main
{
static StringBuilder output = new StringBuilder();
static List<String> al=new ArrayList<>();
public static void combine(String str,int start,int n)
{
for( int i = start; i < str.length(); ++i )
{
output.append(str.charAt(i));
if(output.length()==n&&(!al.contains(output.toString())))
{
al.add(output.toString());
}
if (i<str.length())
{
combine(str,i+1,n);
}
output.setLength(output.length()-1 );
}
}
public static void main (String args[])
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int n=sc.nextInt();
combine(str,0,n);
Collections.sort(al);
for(int index=0;index<al.size();index++)
{
System.out.println(al.get(index));
}
}
}
input 1:
lemon
4
output 1:
emon
lemn
lemo
leon
lmon
input 2:
apple
3
output 2:
ale
ape
apl
app
ple
ppe
ppl
4) String rotation based on the queries
import java.util.*;
public class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int n=sc.nextInt();
for(int index=1;index<=n;index++)
{
char ch=sc.next().charAt(0);
int k=sc.nextInt();
int l=str.length();
if(k>l)
{
k=k%l;
}
if(ch=='L')
{
str=str.substring(k,l)+str.substring(0,k);
}
else if(ch=='R')
{
str=str.substring(l-k,l)+str.substring(0,l-k);
}
System.out.print(str.charAt(0));
}
}
}
input 1:
skillrack
3
L 3
R 2
L 2
output 1:
lkl
String replaced with astriesk pattern
import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String str=sc.next();
int l=str.length();
int d=0;
int m=n;
for(int index=1;index<=n;index++)
{
StringBuilder sb1=new StringBuilder();
d=m;
for(int i=0;i<l;i++)
{
if(i==d-1)
{
sb1.append('*');
d=d+n;
}
else
{
sb1.append(str.charAt(i));
}
}
m--;
String str1=sb1.toString();
System.out.println(str1);
str=str1.substring(0,l);
}
}
}
input 1:
3
skillrack
output 1:
sk*ll*ac*
s**l**a**
*********
input 2:
5
keyboard
output 2:
keyb*ard
key**ard
ke***ar*
k****a**
********
Find the number of segments required to display the given number
import java.util.*;
public class Main
{
static int findSegment(char ch)
{
int num=0;
if(ch=='0'||ch=='6'||ch=='9')
num=6;
else if(ch=='1')
num=2;
else if(ch=='2'||ch=='3'||ch=='5')
num=5;
else if(ch=='4')
num=4;
else if(ch=='7')
num=3;
else if(ch=='8')
num=7;
return num;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
int sum=0;
for(int index=0;index<l;index++)
{
sum+=(findSegment(str.charAt(index)));
}
System.out.print(sum);
}
}
input 1:
88
output 1:
14
input 2:
999999
output 2:
36
input 3:
31
output 3:
7
Minimum deletion to form an anagram
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
static int findAnagrams(String str1,String str2)
{
int l1=str1.length();
int l2=str2.length();
int[] freq1=new int[26];
int[] freq2=new int[26];
for(int index=0;index<l1;index++)
{
freq1[str1.charAt(index)-'a']++;
}
for(int index=0;index<l2;index++)
{
freq2[str2.charAt(index)-'a']++;
}
int result=0;
for(int index=0;index<26;index++)
{
result+=Math.abs(freq1[index]-freq2[index]);
}
return result;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str1=sc.next();
String str2=sc.next();
System.out.print(findAnagrams(str1,str2));
}
}
input 1:
bcadeh
hea
output 1:
3
Find whether it is a Shuffle of two string
import java.util.*;
public class Main {
static boolean shuffle(String a,String b,String c)
{
StringBuffer sb1=new StringBuffer(a);
StringBuffer sb2=new StringBuffer(b);
StringBuffer sb3=new StringBuffer(c);
int l1=sb1.length();
int l2=sb2.length();
int l3=sb3.length();
while(l3>0)
{
if(l1>0&&(sb1.charAt(0)==sb3.charAt(0)))
{
sb1.deleteCharAt(0);
sb3.deleteCharAt(0);
l1--;
l3--;
}
else if(l2>0&&(sb2.charAt(0)==sb3.charAt(0)))
{
sb2.deleteCharAt(0);
sb3.deleteCharAt(0);
l2--;
l3--;
}
else
{
return false;
}
}
if(l1==0&&l2==0&&l3==0)
{
return true;
}
else
{
return false;
}
}
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
String a=sc.next();
String b=sc.next();
String c=sc.next();
System.out.print(shuffle(a,b,c));
}
}
input 1:
abcd
efgh
aebfgcdh
output 1:
true
Find last occuring maximum element in a string
import java.util.*;
public class Main{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
Map<Character,Integer> hm=new LinkedHashMap<Character,Integer>();
int count=0;
for(int index=0;index<l;index++)
{
if(hm.containsKey(str.charAt(index)))
{
count=hm.get(str.charAt(index));
hm.put(str.charAt(index),++count);
}
else
{
hm.put(str.charAt(index),1);
}
}
List<Character> al=new ArrayList<Character>();
int max=0;
for(Map.Entry<Character,Integer> m:hm.entrySet())
{
if(m.getValue()>max)
{
max=m.getValue();
}
}
for(Map.Entry<Character,Integer> m:hm.entrySet())
{
if(m.getValue()==max)
{
al.add(m.getKey());
}
}
System.out.print(al.get(al.size()-1));
}
}
input 1:
management
output 1:
e
input 2:
enhancement
output 2:
e
Minimum deletions to make the strings alternate
import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
static int FindCount(String str,int l)
{
int result=0;
for(int index=0;index<l-1;index++)
{
if(str.charAt(index)==str.charAt(index+1))
{
result++;
}
}
return result;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
System.out.println(FindCount(str,l));
}
}
input 1:
ababaaaab
output 1:
3
input 2:
ababab
output 2:
0
Insert word in lexicographic order
import java.util.*;
public class Main {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
List<String> al=new ArrayList<String>();
while(sc.hasNext())
{
al.add(sc.next());
}
Collections.sort(al);
for(String s:al)
{
System.out.print(s+" ");
}
}
}
input 1:
abacus ball hat mind zebra
link
output 1:
abacus ball hat link mind zebra
Count the common characters in two strings
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
char[] str1 = sc.nextLine().toCharArray();
char[] str2 = sc.nextLine().toCharArray();
int l1=str1.length;
int l2=str2.length;
List<Character> List = new ArrayList<>();
for(int index1=0;index1<l1;index1++)
{
for(int index2=0;index2<l2;index2++)
{
if(str1[index1]==str2[index2])
{
if(!List.contains(str1[index1]))
{
List.add(str1[index1]);
}
}
}
}
System.out.println("\nNumber of common character : "+List.size());
}
}
input 1:
india
china
output 1:
Number of common character : 3
Print alternate vowels from first and last
import java.util.*;
public class Main
{
static boolean isVowel(char ch)
{
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
return true;
else
return false;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
List<Character> al=new ArrayList<>();
for(int index=0;index<l;index++)
{
char ch=str.charAt(index);
if(isVowel(ch))
al.add(ch);
}
int len=al.size();
int start=0;
int end=len-1;
while(start<=end)
{
if(start==end)
System.out.print(al.get(start));
else
System.out.print(al.get(start)+""+al.get(end));
start++;
end--;
}
}
}
input 1:
skillrack
output 1:
ia
input 2:
managerial
output 2:
aaaie
Sort adjacent words of the given string
import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String []str=sc.nextLine().split(" ");
String word=sc.next();
for(int index=1;index<str.length-1;index++)
{
if(str[index].equals(word))
{
String temp=str[index-1];
str[index-1]=str[index+1];
str[index+1]=temp;
}
}
for(int index=0;index<str.length;index++)
{
System.out.print(str[index]+" ");
}
}
}
input 1:
welcome to codejamm, we welcome you to the best learning platform
to
output 2:
codejamm, to welcome we welcome the to you best learning platform
Find the Longest Palindromic substring and its length
import java.util.*;
public class MyClass
{
static int longestPalSubstr(String str)
{
int n = str.length();
boolean table[][] = new boolean[n][n];
int maxLength = 1;
for (int i = 0; i < n; ++i)
{
table[i][i] = true;
}
int start = 0;
for (int i = 0; i < n - 1; ++i)
{
if (str.charAt(i) == str.charAt(i + 1))
{
table[i][i + 1] = true;
start = i;
maxLength = 2;
}
}
for (int k = 3; k <= n; ++k)
{
for (int i = 0; i < n - k + 1; ++i)
{
int j = i + k - 1;
if (table[i + 1][j - 1] && str.charAt(i) == str.charAt(j))
{
table[i][j] = true;
if (k > maxLength)
{
start = i;
maxLength = k;
}
}
}
}
int low=start;
int high=start+maxLength-1;
String ans=str.substring(low,high+1);
System.out.println("Longest palindrome substring is: "+ans);
return maxLength;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str = sc.next();
System.out.println("Length is: " +
longestPalSubstr(str));
}
}
input 1:
abadabe
output 2:
Longest palindrome substring is: badab
Length is: 5
sort the characters at given position and replace them in the same string
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int len=sc.nextInt();
int size=sc.nextInt();
String str=sc.next();
char[] x=str.toCharArray();
List<Integer> al=new ArrayList<>();
for(int i=0;i<size;i++)
{
al.add(sc.nextInt());
}
int k=0;
List<Character> arr=new ArrayList<>();
for(int index=0;index<len;index++)
{
if(index==(al.get(k)-1))
{
arr.add(str.charAt(index));
k++;
}
}
k=0;
Collections.sort(arr);
for(int index=0;index<len;index++)
{
if(index==(al.get(k)-1))
{
x[index]=arr.get(k);
k++;
}
System.out.print(x[index]);
}
}
}
input 1:
7 3
acbdaxa
2 4 7
output 1:
aabcaxd
explanation:
the string is given and number of indexs is given
the characters at that position in the string is to be sorted and then relaced in the same string
eg: character at position in string "acbdaxa" is 2-->c , 4-->d , 7-->a
this is sorted as [ a , c , d]
then its is replaced in the string at the same positons. hence the output is "aabcaxd"
Interlaced digits printing in reverse order
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str1=sc.next();
String str2=sc.next();
int l1=str1.length();
int l2=str2.length();
int index=0;
List<Integer> arr=new ArrayList<>();
while(l1!=0 && l2!=0)
{
arr.add(str1.charAt(index)-'0');
l1--;
arr.add(str2.charAt(index)-'0');
l2--;
index++;
}
while(l1!=0)
{
arr.add(str1.charAt(index)-'0');
l1--;
index++;
}
while(l2!=0)
{
arr.add(str2.charAt(index)-'0');
l2--;
index++;
}
for(int index1=arr.size()-1;index1>=0;index1--)
{
System.out.print(arr.get(index1));
}
}
}
input 1:
123
456
output 1:
635241
input 2:
12345
678
output 2:
54837261
Sort the String based on the count of consonants
import java.util.*;
public class Main
{
static int countConsonants(String str)
{
int l=str.length();
int count=0;
for(int index=0;index<l;index++)
{
char ch=str.charAt(index);
if(!(ch=='a'|ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'))
{
count++;
}
}
return count;
}
static HashMap<String,Integer> sortValues(HashMap<String,Integer> hm)
{
List<Map.Entry<String,Integer>> list=new ArrayList<Map.Entry<String,Integer>>(hm.entrySet());
Collections.sort(list,new Comparator<Map.Entry<String,Integer>>()
{
public int compare(Map.Entry<String,Integer> str1,Map.Entry<String,Integer> str2)
{
return (str1.getValue()).compareTo(str2.getValue());
}
});
HashMap<String,Integer> temp=new LinkedHashMap<>();
for(Map.Entry<String,Integer> tm:list)
{
temp.put(tm.getKey(),tm.getValue());
}
return temp;
}
public static void main(String[] args)
{
HashMap<String,Integer> hm=new LinkedHashMap<>();
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int index=1;index<=n;index++)
{
String str=sc.next();
int count=countConsonants(str);
hm.put(str,count);
}
Map<String,Integer> hm1=sortValues(hm);
for(Map.Entry<String,Integer> m:hm1.entrySet())
{
System.out.print(m.getKey()+" ");
}
}
}
input 1:
5
Early bird catches the worm
output 1:
the Early bird worm catches
explanation:
sort the strings based on the count of consonants. if there are same number of consonants for more than one string,
the sort them based on the order of occurence.
input 2:
4
Robot computing cloud lab
output 2:
lab Robot cloud computing
Find the missing lower case alphabet in the given two strings
import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str1=sc.next();
String str2=sc.next();
String str=str1+str2;
int l=str.length();
char[] arr={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
for(int index=0;index<l;index++)
{
char c=str.charAt(index);
for(int i=0;i<26;i++)
{
if(c==arr[i])
{
arr[i]='*';
break;
}
}
}
int flag=0;
for(int index=0;index<26;index++)
{
if(arr[index]!='*')
{
System.out.print(arr[index]);
flag=1;
}
}
if(flag==0)
System.out.print("-1");
}
}
input 1:
hello
skillrack
output 1:
bdfgjmnpqtuvwxyz
Words sorted or not based on length of the words
import java.util.*;
public class Main
{
static HashMap<String,Integer> sortValues(HashMap<String,Integer> hm)
{
List<Map.Entry<String,Integer>> list=new ArrayList<Map.Entry<String,Integer>>(hm.entrySet());
Collections.sort(list,new Comparator<Map.Entry<String,Integer>>()
{
public int compare(Map.Entry<String,Integer> str1,Map.Entry<String,Integer> str2)
{
return (str1.getValue()).compareTo(str2.getValue());
}
});
HashMap<String,Integer> temp=new LinkedHashMap<>();
for(Map.Entry<String,Integer> tm:list)
{
temp.put(tm.getKey(),tm.getValue());
}
return temp;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
HashMap<String,Integer> hm=new LinkedHashMap<>();
String str[]=sc.nextLine().split(" ");
int l=str.length;
for(int i=0;i<l;i++)
{
String str1=str[i];
int length=str1.length();
hm.put(str1,length);
}
Map<String,Integer> hm1=sortValues(hm);
List<String> str2=new ArrayList<>();
for(Map.Entry<String,Integer> m:hm1.entrySet())
{
str2.add(m.getKey());
}
int flag=0;
for(int index=0;index<l;index++)
{
if(str2.get(index).equals(str[index]))
{
flag++;
}
}
if(flag==l)
System.out.print("YES");
else
System.out.print("NO");
}
}
input 1:
The moon looks beautiful
output 1:
YES
input 2:
I am a Doctor
output 2:
NO
Form String -from the concatenation of previous strings
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str1=sc.next();
String str2=sc.next();
String str=sc.next();
int l=str.length();
List<String> al=new ArrayList<>();
for(int index=0;index<l;index+=2)
{
al.add(str.substring(index,index+2));
}
int flag=0;
for(int index=0;index<al.size();index++)
{
if((al.get(index).equals(str1))||(al.get(index).equals(str2)))
{
flag++;
}
}
if(flag==l/2)
System.out.print("YES");
else
System.out.print("NO");
}
}
input 1:
44 48
4448444448
output 1:
YES
input 2:
15 10
10151051101015
output 2:
NO
Count of characters of s1 not present in s2:
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str1=sc.next();
String str2=sc.next();
int l1=str1.length();
int l2=str2.length();
int freq[]=new int[256];
for(int index=0;index<l1;index++)
{
freq[str1.charAt(index)]++;
}
int count=0;
for(int index=0;index<l2;index++)
{
if(freq[str2.charAt(index)]==0)
{
count++;
}
else
{
freq[str2.charAt(index)]--;
}
}
System.out.print(count);
}
}
input:
skillrack
rockstar
output:
3
Longest Anagram sequence length
import java.util.*;
public class Main
{
public static int findCount (String str1, String str2)
{
int l1 = str1.length ();
int l2 = str2.length ();
int[] freq1 = new int[26];
int[] freq2 = new int[26];
Arrays.fill (freq1, 0);
Arrays.fill (freq2, 0);
for (int index1 = 0; index1 < l1; index1++)
{
freq1[str1.charAt (index1) - 'a']++;
}
for (int index2 = 0; index2 < l2; index2++)
{
freq2[str2.charAt (index2) - 'a']++;
}
int count = 0;
for (int index = 0; index < 26; index++)
{
count += Math.min (freq1[index], freq2[index]);
}
return count;
}
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
String str1 = sc.next ();
String str2 = sc.next ();
System.out.print (findCount (str1, str2));
}
}
input:
abdacp
ckamb
output:
3
Sort the characters at given position and replace in the string
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int len=sc.nextInt();
int size=sc.nextInt();
String str=sc.next();
char[] x=str.toCharArray();
List<Integer> al=new ArrayList<>();
for(int i=0;i<size;i++)
{
al.add(sc.nextInt());
}
int k=0;
List<Character> arr=new ArrayList<>();
for(int index=0;index<len;index++)
{
if(index==(al.get(k)-1))
{
arr.add(str.charAt(index));
k++;
}
}
k=0;
Collections.sort(arr);
for(int index=0;index<len;index++)
{
if(index==(al.get(k)-1))
{
x[index]=arr.get(k);
k++;
}
System.out.print(x[index]);
}
}
}
input:
7 3
acbdaxa
2 4 7
output:
aabcaxd
explanation:
the string is given and number of indexs is given
the characters at that position in the string is to be sorted and then relaced in the same string
eg: character at position in string "acbdaxa" is 2-->c , 4-->d , 7-->a
this is sorted as [ a , c , d]
then its is replaced in the string at the same positons. hence the output is "aabcaxd"
Inverted U pattern of three strings
import java.util.*;
public class Main
{
static String[] findTopString (String str1, String str2, String str3)
{
String str[] = new String[3];
int l1 = str1.length ();
int l2 = str2.length ();
int l3 = str3.length ();
if (str1.charAt (0) == str2.charAt (0)
&& str1.charAt (l1 - 1) == str3.charAt (0))
{
str[0] = str1;
str[1] = str2;
str[2] = str3;
}
else if (str1.charAt (0) == str3.charAt (0)
&& str1.charAt (l1 - 1) == str2.charAt (0))
{
str[0] = str1;
str[1] = str3;
str[2] = str2;
}
else if (str2.charAt (0) == str1.charAt (0)
&& str2.charAt (l2 - 1) == str3.charAt (0))
{
str[0] = str2;
str[1] = str1;
str[2] = str3;
}
else if (str2.charAt (0) == str3.charAt (0)
&& str2.charAt (l2 - 1) == str1.charAt (0))
{
str[0] = str2;
str[1] = str3;
str[2] = str1;
}
else if (str3.charAt (0) == str1.charAt (0)
&& str3.charAt (l3 - 1) == str2.charAt (0))
{
str[0] = str3;
str[1] = str1;
str[2] = str2;
}
else if (str3.charAt (0) == str2.charAt (0)
&& str3.charAt (l3 - 1) == str1.charAt (0))
{
str[0] = str3;
str[1] = str2;
str[2] = str1;
}
return str;
}
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
String str1 = sc.next ();
String str2 = sc.next ();
String str3 = sc.next ();
String str[] = findTopString (str1, str2, str3);
int l1 = str[0].length ();
int l2 = str[1].length ();
System.out.println (str[0]);
for (int row = 1; row < l2; row++)
{
System.out.print (str[1].charAt (row));
for (int col = 0; col < l1 - 2; col++)
{
System.out.print ("*");
}
System.out.print (str[2].charAt (row));
System.out.println ();
}
}
}
input 1:
PANTHER
PATNA
ALLERGY
output 1:
PATNA
A***L
N***L
T***E
H***R
E***G
R***Y
explanation:
the string can be of same lengths or diffrent lengths. but the strings at both the sides are only of same lengths
and the first character of left string is the first caharcter of top string and
the first character of right string is the last character of to string
input 2:
MANGOES
MICHALE
ECYTACS
output 2:
MICHALE
A*****C
N*****Y
G*****T
O*****A
E*****C
S*****S
Find the mismatch character present in the sequence
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str1=sc.next();
char[] x=str1.toCharArray();
Arrays.sort(x);
String str=new String(x);
char ch='\0';
for(int index=0;index<str.length();index++)
{
StringBuilder temp=new StringBuilder(str);
temp.deleteCharAt(index);
int diff=temp.charAt(1)-temp.charAt(0);
boolean flag=true;
for(int index2=0;index2<temp.length()-1;index2++)
{
if(temp.charAt(index2+1)-temp.charAt(index2)!=diff)
{
flag=false;
break;
}
}
if(flag==true)
{
ch=str.charAt(index);
}
}
System.out.print(ch);
}
}
input:
acreg
output:
r
Segregate uppercase and lowercase from the string
import java.util.*;
public class MyClass
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
StringBuilder sb1=new StringBuilder(str);
StringBuilder uppercase=new StringBuilder();
int index=0;
while(index<l)
{
if(Character.isUpperCase(sb1.charAt(index)))
{
uppercase.append(sb1.charAt(index));
sb1.deleteCharAt(index);
l--;
index--;
}
else
index++;
}
System.out.println(uppercase.toString());
System.out.print(sb1.toString());
}
}
input 1:
abCDEfgHI
output 1:
CDEHI
abfg
Longest Palindrome formed by shuffling or deleting Characters
import java.util.*;
public class Main
{
static String findPalindrome(String str)
{
Map<Character,Integer> freq = new LinkedHashMap<>();
int count=0;
for (int index=0;index<str.length();index++)
{
count=0;
if(freq.containsKey(str.charAt(index)))
{
count=freq.get(str.charAt(index));
freq.put(str.charAt(index),count+1);
}
else
{
freq.put(str.charAt(index),1);
}
}
char middle = '\0';
StringBuilder left = new StringBuilder();
for (Map.Entry<Character, Integer> m: freq.entrySet())
{
char ch = m.getKey();
int counter = m.getValue();
if (counter % 2 == 1)
{
middle = ch;
}
for (int index = 0; index < counter/2; index++)
{
left.append(ch);
}
}
StringBuilder sb=new StringBuilder(left);
String right = sb.reverse().toString();
return (left.toString() + middle + right);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str = sc.next();
System.out.print("The Longest Palindrome by shuffling or deleting characters:"+findPalindrome(str));
}
}
input 1:
bananas
output 1:
The Longest Palindrome by shuffling or deleting characters:ansna
input 2:
abbdab
output 2:
The Longest Palindrome by shuffling or deleting characters:abdba
Replace all non-overlapping occurrences by given ch in the pattern
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str = sc.next();
String replaceStr=sc.next();
String ch=sc.next();
System.out.print(str.replace(replaceStr,ch));
}
}
input 1:
abcyabcyabc
abc
@
output 1:
@y@y@
input 2:
abcyabcyabc
abc
@
output 2:
#y#y#
Find the pattern occurrence index in the given sentence
import java.util.*;
public class Binary
{
static void search(String str,String pattern)
{
int l1=str.length();
int l2=pattern.length();
for(int index=0;index<l1-l2+1;index++)
{
if(str.charAt(index)==pattern.charAt(0))
{
String str1=str.substring(index,index+l2);
if(str1.equals(pattern))
{
System.out.println("Pattern is found at index "+index);
}
}
}
}
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the input:");
String str=sc.nextLine();
System.out.println("Enter the pattern:");
String pattern=sc.next();
search(str,pattern);
}
}
Input 1:
Enter the input:
this is a test input
Enter the pattern:
test
Output 1:
Pattern is found at index 10
Input 2:
Enter the input:
AABAACAADAABAABA
Enter the pattern:
AABA
Output 2:
Pattern is found at index 0
Pattern is found at index 9
Pattern is found at index 12
Print the character from last in increasing order sequence:
import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
StringBuilder sb=new StringBuilder(str);
String str1=sb.reverse().toString();
int l=str1.length();
int m=0;
int[] arr={1,3,6,10,15,21,28,36,45,55,66,78,91,105,120,136,153,171,190};
for(int index=0;index<l;index++)
{
if(index==arr[m]-1)
{
System.out.print(str1.charAt(index));
m++;
}
}
}
}
input 1:
abcdefghijklmnopqrst
output 1:
trokf
explanation:
The length of string is <200 and the character prints from the last at position first,third,fifth,tenth,fifteenth,and so on... in a constant increasing order sequence
input 2:
skillrack
output 2:
kal
String repeating elements in-place sort
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str=sc.next();
int l=str.length();
int count=0;
List<Character>al= new ArrayList<>();
Map<Character,Integer>hm= new LinkedHashMap<>();
for(int index=0;index<l;index++)
{
count=0;
if(hm.containsKey(str.charAt(index)))
{
count=hm.get(str.charAt(index));
hm.put(str.charAt(index),count+1);
}
else
{
hm.put(str.charAt(index),1);
}
}
for(Map.Entry<Character,Integer> m:hm.entrySet())
{
if(m.getValue()>1)
{
for(int i=0;i<m.getValue();i++)
{
al.add(m.getKey());
}
}
}
Collections.sort(al);
int k=0;
for(int index=0;index<l;index++)
{
if(al.contains(str.charAt(index)))
{
System.out.print(al.get(k++));
}
else
{
System.out.print(str.charAt(index));
}
}
}
}
input 1:
tattarrattat
output 1:
aaaarrtttttt
input 2:
skillrack
output 2:
skiklracl
Next immediate number greater than Y
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String suffix=sc.next();
int Y=sc.nextInt();
Set<Integer> list=new TreeSet<>();
permute("",suffix,Y,list);
if(list.size()>0)
{
for(Integer val:list)
{
System.out.print(val);
break;
}
}
else
System.out.print("-1");
}
public static void permute(String prefix,String suffix,int check,Set<Integer> list)
{
if(suffix.length()==0)
{
int val=Integer.parseInt(prefix);
if(val>check)
{
if(!list.contains(val)) list.add(val);
}
}
else
{
int len=suffix.length();
for(int itr=0;itr<len;itr++)
{
permute(prefix+suffix.charAt(itr),suffix.substring(0,itr)+suffix.substring(itr+1,len),check,list);
}
}
}
}
input 1:
4569 5000
output 1:
5469
input 2:
6387 8000
output 2:
8367
Find the Abbreviation in the given pattern of strings
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
sc.nextLine();
String[] str=new String[num];
for(int i=0;i<num;i++)
{
str[i]=sc.nextLine();
}
String cmp=sc.nextLine().trim();
for(String s:str)
{
int ctr=0,f=0;
for (String ss:s.split(" "))
{
if (Character.toLowerCase(cmp.charAt(ctr++))!=Character.toLowerCase(ss.charAt(0)))
{
f=1;
break;
}
}
if (f==0 && ctr==cmp.length())
{
System.out.print(s);
return;
}
}
}
}
input 1:
4
hellow worlds
World Health Organisation
Wolrd Social events
Welcome to CodeJamm
WHO
output 1:
World Health Organisation
java program to check fibonacci sequence or not
.
import java.util.*;
public class Hello
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
if(l==1)
{
System.out.print("YES");
System.exit(0);
}
Map<Character,Integer> hm=new LinkedHashMap<Character,Integer>();
int count=0;
for(int index=0;index<l;index++)
{
count=0;
if(hm.containsKey(str.charAt(index)))
{
count=hm.get(str.charAt(index));
hm.put(str.charAt(index),++count);
}
else
{
hm.put(str.charAt(index),1);
}
}
int count1=0;
List<Integer> al=new ArrayList<Integer>();
for(Map.Entry<Character,Integer> m:hm.entrySet())
{
count1++;
al.add(m.getValue());
}
Collections.sort(al);
int[] dp=new int[count1];
dp[0]=1;
dp[1]=1;
for(int index=2;index<count1;index++)
{
dp[index]=dp[index-1]+dp[index-2];
}
int flag=0;
for(int index=0;index<count1;index++)
{
if(al.get(index)==dp[index]||dp[0]==0)
{
flag++;
}
}
if(flag==count1)
{
System.out.print("YES");
}
else
{
System.out.print("NO");
}
}
}
sample input:
abbcccd
sample output:
YES
Java program to find the Longest Common SubString amoung the two given strings
import java.util.*;
public class Hello {
static void printLCSubStr(String X, String Y, int m, int n)
{
int[][] LCSuff = new int[m + 1][n + 1];
int len = 0;
int row = 0, col = 0;
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0)
LCSuff[i][j] = 0;
else if (X.charAt(i - 1) == Y.charAt(j - 1)) {
LCSuff[i][j] = LCSuff[i - 1][j - 1] + 1;
if (len < LCSuff[i][j]) {
len = LCSuff[i][j];
row = i;
col = j;
}
}
else
LCSuff[i][j] = 0;
}
}
if (len == 0) {
System.out.println("No Common Substring");
return;
}
String resultStr = "";
while (LCSuff[row][col] != 0) {
resultStr = X.charAt(row - 1) + resultStr;
--len;
row--;
col--;
}
System.out.println(resultStr);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String X=sc.nextLine();
String Y=sc.nextLine();
int m = X.length();
int n = Y.length();
printLCSubStr(X, Y, m, n);
}
}
sample input:
skillrack
SkillRack
sample output:
kill
Simple Calculator Command:
import java.util.*;
public class Main
{
static void calculation(int num1,int num2,char c)
{
if(c=='a'||c=='A')
System.out.print(num1+num2);
else if(c=='S'||c=='s')
System.out.print(num1-num2);
else if(c=='M'||c=='m')
System.out.print(num1*num2);
else if(c=='d'||c=='D')
System.out.print(num1/num2);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
String str1="",str2="";
char c='\0';
for(int index=0;index<l;index++)
{
char ch=str.charAt(index);
if(!(ch>='0'&&ch<='9'))
{
str1=str.substring(0,index);
str2=str.substring(index+1,l);
c=ch;
break;
}
}
calculation(Integer.parseInt(str1),Integer.parseInt(str2),c);
}
}
input 1:
1405d10
output 1:
140
input 2:
5A11
output 2:
16
String Triangle Pattern:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
for(int row=0;row<l-2;row++)
{
String str1="";
int size1=l-row-2,size2=row+1;
for(int col=0;col<l;col+=2)
{
int end1=size1,end2=size2;
while(end1>0)
{
str1+=str.charAt(col);
end1--;
}
while(end2>0)
{
if(col<l-1)
str1+=str.charAt(col+1);
else
str1+='*';
end2--;
}
int temp=size1;
size1=size2;
size2=temp;
}
System.out.println(str1);
}
}
}
input 1:
skill
output 1:
ssskillllll*
sskkiillll**
skkkiiill***
input 2:
manage
output 2:
mmmmanaaaagggge
mmmaannaaagggee
mmaaannnaaggeee
maaaannnnageeee
input 3:
fluorescent
output 3:
fffffffffluooooooooorrrrrrrrresccccccccceeeeeeeeent*********
fffffffflluuoooooooorrrrrrrreesscccccccceeeeeeeenntt********
fffffffllluuuooooooorrrrrrreeesssccccccceeeeeeennnttt*******
fffffflllluuuuoooooorrrrrreeeesssscccccceeeeeennnntttt******
fffffllllluuuuuooooorrrrreeeeesssssccccceeeeennnnnttttt*****
fffflllllluuuuuuoooorrrreeeeeesssssscccceeeennnnnntttttt****
fffllllllluuuuuuuooorrreeeeeeesssssssccceeennnnnnnttttttt***
fflllllllluuuuuuuuoorreeeeeeeesssssssscceennnnnnnntttttttt**
fllllllllluuuuuuuuuoreeeeeeeeessssssssscennnnnnnnnttttttttt*
Count of Common Characters in two strings:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
String str1=in.next();
String str2=in.next();
int count=0;
char a[] =str1.toCharArray();
char b[] =str2.toCharArray();
Set<Character> sb=new HashSet<>();
for(int i=0;i<a.length;i++)
{
sb.add(a[i]);
}
for(int i=0;i<b.length;i++)
{
if(sb.contains(b[i]))
{
count++;
sb.remove(b[i]);
}
}
System.out.print(count);
}
}
input 1:
kingkong
zest
output 1:
0
input 2:
china
india
output 2:
3
intput 3:
energy
every
output 3:
3
Count the articles in a string:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str[]=sc.nextLine().split(" ");
int count=0;
for(int index=0;index<str.length;index++)
{ if(str[index].equals("a")||str[index].equals("an")||str[index].equals("the")||str[index].equals("a,")||str[index].equals("a.")||str[index].equals("a.")||str[index].equals("an,")||str[index].equals("an.")||str[index].equals("the.")||str[index].equals("the,"))
{
count++;
}
}
System.out.print(count);
}
}
input 1:
the count of a an , the function peoples are huge in number than the estimated count
output 1:
5
explanation:
The string need not to be grammatical.
Count the number of sub-Palindromes in a string:
import java.util.*;
public class Main
{
static int countPalindromes(String str)
{
int l=str.length(),count=0;
String temp="";
for(int index1=0;index1<l;index1++)
{
for(int index2=index1+1;index2<=l;index2++)
{
temp=str.substring(index1,index2);
if(temp.length()>=2)
{
StringBuilder sb=new StringBuilder(temp);
sb.reverse();
if(sb.toString().compareTo(temp)==0)
{
count++;
}
}
}
}
return count;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
System.out.print(countPalindromes(str));
}
}
input 1:
everest
output 1:
2
input 2:
codedoc
output 2:
3
Next Number Palindrome:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int n=Integer.parseInt(str);
for(int index=n+1;index<=n+1000;index++)
{
String str1=Integer.toString(index);
StringBuilder sb=new StringBuilder(str1);
sb=sb.reverse();
if(str1.equals(sb.toString()))
{
System.out.print(str1);
break;
}
}
}
}
input 1:
900
output 1:
909
input 2:
12567
output 2:
12621
input 3:
123
output 3:
131
Sort the rows based on its equivalent binary values:
import java.util.*;
public class Hello
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int R=sc.nextInt();
int C=sc.nextInt();
List<String> al=new ArrayList<>();
int index=0;
for(int index1=1;index1<=R+1;index1++)
{
String str=sc.nextLine();
String[] arr=str.split(" ");
String str1="";
for(int index2=0;index2<arr.length;index2++)
{
str1=str1+arr[index2];
}
al.add(str1);
}
Collections.sort(al);
for(int index1=1;index1<=R;index1++)
{
String temp=al.get(index1);
for(int index2=0;index2<temp.length();index2++)
{
System.out.print(temp.charAt(index2)+" ");
}
System.out.println();
}
}
}
input 1:
5 3
1 1 1
0 1 0
0 1 0
0 0 1
1 0 0
output 1:
0 0 1
0 1 0
0 1 0
1 0 0
1 1 1
explanation:
the binary to decimal value row-wise is 7,2,2,1,4
on sorting this, and sorting the corresponsind rows, the final matrix is printed.
Integers with atleast 3 distinct digits:
import java.util.*;
public class Main
{
static boolean findDistinctCount(String str)
{
Set<Character> st=new HashSet<>();
int l=str.length();
for(int index=0;index<l;index++)
{
st.add(str.charAt(index));
}
if(st.size()>=3)
return true;
else
return false;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int flag=0;
for(int index=1;index<=n;index++)
{
String str=sc.next();
if(findDistinctCount(str))
{
System.out.print(str+" ");
flag=1;
}
}
if(flag==0)
System.out.print("-1");
}
}
input 1:
5
56 65789 10 4567 325
output 1:
65789 4567 325
input 2:
8
23 56739 77789 253 67 5 35648 2000456
output 2:
56739 77789 253 35648 2000456
Longest Capitalized Substring:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
List<String> al=new ArrayList<>();
int ctr=0;
for(int index1=0;index1<l;index1++)
{
ctr=index1;
if(Character.isUpperCase(str.charAt(index1)))
{
for(int index2=index1+1;index2<l;index2++)
{
if(Character.isUpperCase(str.charAt(index2)))
{
ctr=index2;
al.add(str.substring(index1,ctr));
break;
}
}
}
}
int max=0,flag=0;
for(int index=0;index<al.size();index++)
{
String s=al.get(index);
if(s.length()>=2&&s.length()>max)
{
max=s.length();
}
}
List<String> al2=new ArrayList<>();
for(int index=0;index<al.size();index++)
{
if(al.get(index).length()==max)
{
al2.add(al.get(index));
flag=1;
}
}
if(flag==0)
System.out.print("-1");
else
System.out.print(al2.get(0));
}
}
input 1:
ThisIsSkillRack
output 1:
Skill
Non Palindromic Words in a Sentence:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String[] arr=str.split(" ");
int flag=0;
for(int index=0;index<arr.length;index++)
{
StringBuilder sb=new StringBuilder(arr[index]);
sb=sb.reverse();
if(!(sb.toString().equals(arr[index])))
{
System.out.print(arr[index]+" ");
flag=1;
}
}
if(flag==0)
System.out.println("-1");
}
}
input 1:
malayalam ere
output 1:
-1
input 2:
madam says malayalam speak mom and dad
output 2:
says speak and
String from Digits:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
List<Character> al=new ArrayList<>();
String str=sc.next();
while(sc.hasNext())
{
al.add(sc.next().charAt(0));
}
int l=str.length();
StringBuilder sb=new StringBuilder();
for(int index=0;index<l;index++)
{
int n=Character.getNumericValue(str.charAt(index));
sb.append(al.get(n));
}
System.out.print(sb.toString());
}
}
input 1:
1870
a b c d e f g h i j
output 1:
biha
input 2:
09874
a b c d e f g h i j k
output 2:
ajihe
First N common characters in two strings:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str1=sc.next();
String str2=sc.next();
int n=sc.nextInt();
int l1=str1.length();
int l2=str2.length();
Set<Character> st=new LinkedHashSet<>();
for(int index1=0;index1<l1;index1++)
{
for(int index2=0;index2<l2;index2++)
{
if(str1.charAt(index1)==str2.charAt(index2))
{
st.add(str1.charAt(index1));
}
}
}
int count=0;
for(Character i:st)
{
System.out.print(i);
count++;
if(count==n)
{
break;
}
}
}
}
input 1:
energyproduction
renevablesource
4
output 1:
enro
Toogle characters at N indexes with preceding vowels ignoring spaces and special characters:
import java.util.*;
public class Main
{
static boolean isvowel(char c)
{
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='I'||c=='E'||c=='O'||c=='U')
return true;
else
return false;
}
static char toogleCase(char c)
{
if(Character.isUpperCase(c))
{
c=Character.toLowerCase(c);
}
else if(Character.isLowerCase(c))
{
c=Character.toUpperCase(c);
}
return c;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
char[] str=s.toCharArray();
int l=str.length;
int num=sc.nextInt();
int count=1;
for(int index=l-1;index>=0;index--)
{
if(str[index]==' '||index==0)
{
continue;
}
if(count==num)
{
count=0;
if(Character.isLetter(str[index]))
{
int j=index-1;
while(str[j]==' ')
{
j--;
}
if(isvowel(str[j]))
{
str[index]=toogleCase(str[index]);
}
}
}
count++;
}
String str1=new String(str);
System.out.print(str1);
}
}
input 1:
fruITS are DeLicioUS!
3
output 1:
fruItS aRe DeLiCiouS!
input 2:
Abc Dedicate fu!g
2
output 2:
ABc DeDiCaTe Fu!g
Username, domain, extension segregation:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
String str1="",str2="",str3="";
int flag=0;
for(int index=0;index<l;index++)
{
if(str.charAt(index)=='@')
{
str1=str.substring(0,index);
for(int index2=index+1;index2<l;index2++)
{
if(str.charAt(index2)=='.')
{
str2=str.substring(index+1,index2);
str3=str.substring(index2+1,l);
flag=1;
break;
}
}
}
if(flag==1)
break;
}
System.out.println(str3);
System.out.println(str2);
System.out.println(str1);
}
}
input 1:
abcd@gmail.com
output 1:
com
gmail
abcd
input 2:
xyz@yahoo.com
output 2:
com
yahoo
xyz
Run Length Decoding:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
StringBuilder output=new StringBuilder();
String temp="";
int index2=0,count=0,val=0;
for(int index=0;index<l;index++)
{
if(Character.isDigit(str.charAt(index)))
{
count++;
if(count==1)
index2=index-1;
temp+=str.charAt(index);
val=Integer.valueOf(temp);
continue;
}
else
{
if(val==0)
{
output.append(str.charAt(index));
}
if(val!=0)
{
for(int ctr=1;ctr<val;ctr++)
{
output.append(str.charAt(index2));
}
index--;
}
temp="";
val=0;
count=0;
}
}
if(val!=0)
{
for(int index=1;index<val;index++)
{
output.append(str.charAt(index2));
}
}
System.out.print(output);
}
}
input 1:
a2c5z4
output 1:
aaccccczzzz
input 2:
a8b12dv3n2
output 2:
aaaaaaaabbbbbbbbbbbbdvvvnn
Remove the consecutive repeating characters from a string:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length(),ctr=0,flag=0;
for(int index1=0;index1<l;index1++)
{
for(int index2=index1+1;index2<l;index2++)
{
if(str.charAt(index1)!=str.charAt(index2))
{
System.out.print(str.charAt(index1));
break;
}
else
{
for(ctr=index1;ctr<l;ctr++)
{
if(str.charAt(ctr)!=str.charAt(ctr+1))
{
flag=1;
break;
}
}
}
if(flag==1)
{
flag=0;
index1=ctr;
break;
}
}
}
System.out.print(str.charAt(l-1));
}
}
input 1:
bookkeeping
output 1:
bping
input 2:
abcdabcd
output 2:
abcdabcd
Append word with same characters of previous word - at last:
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str[]=sc.nextLine().split(" ");
int l=str.length;
String newStr="";
for(int index=0;index<l-1;index++)
{
if(newStr.length()==0)
{
newStr=str[index];
}
String tempStr=str[index+1];
if(tempStr.charAt(0)==newStr.charAt(newStr.length()-1))
{
newStr=tempStr+newStr;
}
else
{
System.out.println(newStr);
newStr="";
}
}
if(newStr.length()==0)
{
newStr=str[l-1];
}
System.out.print(newStr);
}
}
input 1:
are all levels lavendar lemon maverick king of gamblers
output 1:
are
lemonlavendarlevelsall
kingmaverick
of
gamblers
explanation:
The program must accept a string S with multiple words as input. for each word, if its first character is equal to the last character of the previous word, then the previous word must be added in the end(append) to that word. else the word must be considered to be printed in the next lines.
input 2:
crab boy yesterday yellow wing game engine eat top
output 2:
boycrab
yellowyesterday
gamewing
eatengine
top
Print alternate characters in unequal strings:
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str1=sc.next();
String str2=sc.next();
int l1=str1.length();
int l2=str2.length();
int index1=0,index2=0;
while(index1<l1&&index2<l2)
{
System.out.print(str1.charAt(index1++));
System.out.print(str2.charAt(index2++));
}
while(index1<l1)
{
System.out.print(Character.toUpperCase(str1.charAt(index1++)));
}
while(index2<l2)
{
System.out.print(Character.toLowerCase(str2.charAt(index2++)));
}
}
}
input 1:
abcd
RSTU
output 1:
aRbScTdU
input 2:
abcd
efghijklmnop12
output 2:
aebfcgdhijklmnop12
Run length decoding type 1:
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
int index2=0;
StringBuilder sb=new StringBuilder();
for(int index1=0;index1<l;index1++)
{
if(Character.isDigit(str.charAt(index1)))
{
for(index2=index1+1;index2<l;index2++)
{
if(Character.isLetter(str.charAt(index2)))
{
break;
}
}
int num=Integer.valueOf(str.substring(index1,index2));
for(int index=0;index<num;index++)
{
sb.append(str.charAt(index2));
}
index1=index2;
}
else
{
sb.append(str.charAt(index1));
}
}
System.out.print(sb.toString());
}
}
input 1:
20a15b2cd5z
output 1:
aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbccdzzzzz
input 2:
4a3b2cd5zxy2m
output 2:
aaaabbbccdzzzzzxymm
Run length decoding -type 2:
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
int index2=0;
StringBuilder sb=new StringBuilder();
for(int index1=0;index1<l-1;index1++)
{
if(Character.isLetter(str.charAt(index1)))
{
if(Character.isDigit(str.charAt(index1+1)))
{
for(index2=index1+1;index2<l;index2++)
{
if(Character.isLetter(str.charAt(index2)))
{
break;
}
}
int num=Integer.valueOf(str.substring(index1+1,index2));
for(int index=0;index<num;index++)
{
sb.append(str.charAt(index1));
}
//index1=index2;
}
else
{
sb.append(str.charAt(index1));
}
}
}
System.out.print(sb.toString());
}
}
input 1:
a2c5z4
output 1:
aaccccczzzz
input 2:
a8b12dv3n2
output 2:
aaaaaaaabbbbbbbbbbbbdvvvnn
string Rotate based on odd/even of the sum of digit squares of the given strings:
import java.util.*;
public class Hello
{
static void rotateLeft(String word)
{
System.out.println(word.substring(2,word.length())+word.substring(0,2));
}
static void rotateRight(String word)
{
System.out.println(word.substring(word.length()-1,word.length())+word.substring(0,word.length()-1));
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String[] str=sc.next().split(",");
for(int index=0;index<str.length;index++)
{
String[] str1=str[index].split(":");
String word=str1[0];
String number=str1[1];
int value=0;
for(int i=0;i<number.length();i++)
{
int n=Integer.valueOf(number.charAt(i));
value+=n*n;
}
if(value%2==1)
rotateLeft(word);
else
rotateRight(word);
}
}
}
input 1:
abcde:234,pqrs:456
output 1:
cdeab
spqr
explanation:
The given string is separated by ',' with n number of words. each word has a string and number separated by ':'. if the sum of digits squares of the umber is odd, then rotate the string left by two position.if it is even, the rotate right by one position.
Sort the vowels in string:
import java.util.*;
public class Hello
{
static boolean isvowel(char c)
{
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
return true;
else
return false;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
StringBuilder sb=new StringBuilder();
List<Integer> al1=new ArrayList<>();
List<Character> al2=new ArrayList<>();
int j=0;
for(int index=0;index<l;index++)
{
if(isvowel(str.charAt(index)))
{
al2.add(str.charAt(index));
al1.add(index);
}
else
{
al1.add(l);
}
}
Collections.sort(al2);
for(int index=0;index<l;index++)
{
if(al1.get(index)==index)
{
sb.append(al2.get(j));
j++;
}
else
{
sb.append(str.charAt(index));
}
}
System.out.print(sb.toString());
}
}
input 1:
skillrack
output 1:
skallrick
input 2:
PROGRAMMING
output 2:
PRAGRIMMONG
Count of Players :
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
String str[] = sc.nextLine().split(" ");
int l=str.length;
Stack<String> s = new Stack<>();
for(int index=0;index<l;index++)
{
if(s.isEmpty())
s.push(str[index]);
else if(s.peek().equals(str[index]))
s.pop();
else
s.push(str[index]);
}
System.out.print(s.size());
}
}
input 1:
5
india australia india india australia
output 1:
1
explanation:
The list should contains unique elements. if not, then remove the elements which are continuous. first india and india gets removed, in next cycle australia and austraia gets removed. and at last it has unique elements, hence the size is printed
input 2:
7
germany france italy india moscow srilanka england
output 2:
7
Count of Vowels in sliding window :
import java.util.*;
public class Main
{
static int isVowel(char c)
{
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
return 1;
else
return 0;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
String str=sc.next();
int l=str.length();
int count=0;
for(int index=0;index<N;index++)
{
if(isVowel(str.charAt(index))==1)
count++;
}
System.out.print(count+" ");
for(int index=N;index<l;index++)
{
count=count-(isVowel(str.charAt(index-N)));
count=count+(isVowel(str.charAt(index)));
System.out.print(count+" ");
}
}
}
input 1:
4
environment
output 1:
2 1 2 2 1 2 1 1
input 2:
2
SKILLRACK
output 2:
0 1 1 0 0 1 1 0
4 digit OTP:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
StringBuilder sb=new StringBuilder();
int l=str.length();
for(int index=1;index<l;index+=2)
{
int n=Character.getNumericValue(str.charAt(index));
int num=n*n;
sb.append(Integer.toString(num));
}
String ans=sb.toString();
if(ans.length()<4)
{
System.out.print("-1");
}
else if(ans.length()==4)
{
System.out.print(ans);
}
else
{
System.out.print(ans.substring(0,4));
}
}
}
input 1:
345271
output 1:
1641
explanation:
the digits at even positions are squared and append together. the 4 digit otp can be formed, print first 4 digits of the Number
else print -1.
Integer sum with Constraints :(INFYTQ)
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String[] str=sc.next().split(",");
StringBuilder sb=new StringBuilder();
long num=0;
int l=str.length;
for(int index=0;index<l;index++)
{
long n=Integer.parseInt(str[index]);
if(n==5)
{
break;
}
else
num+=n;
}
for(int index=0;index<l;index++)
{
long n=Integer.parseInt(str[index]);
if(n==8)
{
for(int i=index+1;i<str.length;i++)
{
num+=Integer.parseInt(str[i]);
}
break;
}
}
int flag=0;
for(int index=0;index<l;index++)
{
long n=Integer.parseInt(str[index]);
if(n==5)
{
for(int i=index;i<l;i++)
{
long m=Integer.parseInt(str[i]);
if(m==8)
{
sb.append(str[i]);
flag=1;
break;
}
else
sb.append(str[i]);
}
}
if(flag==1)
break;
}
long num1=Long.parseLong(sb.toString());
System.out.print(num1+num);
}
}
input 1:
3,2,5,6,7,8,9,3
output 1:
5695
explanation:
the integers before 5 adn after 8 are added as num --> 3 + 2 + 9 + 3 = 17
the integers within 5 and 8 are added includng it are appended and converted ad integer --> 5 + 6 + 7 + 8 = 5678
adding both gives 5678 + 17 = 5695 as output.
Remove repeated characters in a string (using Map):
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
int count=0;
Map<Character,Integer> hm=new LinkedHashMap<>();
for(int index=0;index<l;index++)
{
if(hm.containsKey(str.charAt(index)))
{
count=hm.get(str.charAt(index));
hm.put(str.charAt(index),++count);
}
else
{
hm.put(str.charAt(index),1);
}
}
for(Map.Entry<Character,Integer> m:hm.entrySet())
{
if(m.getValue()==1)
{
System.out.print(m.getKey());
}
}
}
}
input 1:
catterpillar
output 1:
cepi
input 2:
skillrack
output 2:
sirac
Remove Repeated characters in a string (using Set):
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str = sc.next();
int l = str.length();
int count=0;
Set<Character> set = new LinkedHashSet<>();
for(int index=0;index<l;index++)
{
if(set.contains(str.charAt(index)))
{
set.remove(str.charAt(index));
}
else
{
set.add(str.charAt(index));
}
}
for(Character ch : set)
{
System.out.print(ch);
}
}
}
Input 1:
engine
Output 1:
gi
Input 2:
caterpillar
Output 2:
ctepi
Reverse every word in a sentence:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String[] str=sc.nextLine().split(" ");
int l=str.length;
for(int index=0;index<l;index++)
{
StringBuilder sb=new StringBuilder(str[index]);
System.out.print(sb.reverse().toString()+" ");
}
}
}
input 1:
friday and saturday
output 1:
yadirf dna yadrutas
input 2:
welcome to codeJamm
output 2:
emoclew ot mmajedoc
Simple Calculator Command:
import java.util.*;
public class Main
{
static void calculation(int num1,int num2,char c)
{
if(c=='a'||c=='A')
System.out.print(num1+num2);
else if(c=='S'||c=='s')
System.out.print(num1-num2);
else if(c=='M'||c=='m')
System.out.print(num1*num2);
else if(c=='d'||c=='D')
System.out.print(num1/num2);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
char c='\0';
StringBuilder sb=new StringBuilder();
for(int index=0;index<l;index++)
{
char ch=str.charAt(index);
if(Character.isLetter(ch))
{
sb.append(str.substring(0,index));
sb.append(",");
sb.append(str.substring(index+1,l));
c=ch;
break;
}
}
String[] arr = sb.toString().split(",");
calculation(Integer.parseInt(arr[0]),Integer.parseInt(arr[1]),c);
}
}
input 1:
-90S-109
output 1:
19
input 2:
150A10
output 2:
160
input 3:
120D40
output 3:
3
Find Maximum length palindromes:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int max=0;
for(int index=0;index<n;index++)
{
String str=sc.next();
StringBuilder sb=new StringBuilder(str);
if(str.equals(sb.reverse().toString()))
{
int l=str.length();
if(l>max)
{
max=l;
}
}
}
System.out.print(max);
}
}
input 1:
5
madam
skillrack
codejamm
eye
dad
output 1:
3
explanation:
the palindromes are madam, eye, dad. the maximum length is 5
Sort string values - first and last:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
List<String> al=new ArrayList<>();
int n=sc.nextInt();
for(int index=0;index<n;index++)
{
al.add(sc.next());
}
Collections.sort(al);
System.out.print(al.get(0)+"\n"+al.get(al.size()-1));
}
}
input 1:
5
tiger elephant giraffee ox monkey
output 1:
elephant
tiger
Print the common ending ans starting part in both strings:
import java.util.*;
public class MyClass
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str1=sc.next();
String str2=sc.next();
int l=str1.length();
String s1="";
for(int i=0;i<l;i++)
{
if(str1.charAt(i)==str2.charAt(0))
{
s1=str1.substring(i,l);
break;
}
}
if(s1.equals(str2.substring(0,s1.length())))
{
System.out.print(s1);
}
}
}
input 1:
redfish
fishtank
output 1:
fish
input 2:
bridge
gear
output 2:
ge
input 3:
mayday
daybreak
output 3:
day
First letter in a word with uppercase:
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
char str[]=sc.nextLine().toCharArray();
int l=str.length;
if(Character.isLowerCase(str[0]))
str[0]=Character.toUpperCase(str[0]);
for(int i=1;i<l;i++)
{
if(str[i]==' ')
{
for(int j=i;j<l;j++)
{
if(str[j]!=' ')
{
if(Character.isLowerCase(str[j]))
str[j]=Character.toUpperCase(str[j]);
break;
}
}
}
}
System.out.print(new String(str));
}
}
input 1:
She is happy
output 1:
She Is Happy
Count of words repeated exactly n times:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
List<String> al=new ArrayList<>();
while(sc.hasNext())
{
al.add(sc.next());
}
int l=al.size();
int n=Integer.valueOf(al.get(l-1));
Map<String,Integer> hm=new LinkedHashMap<>();
int count=0;
for(int index=0;index<l-1;index++)
{
count=0;
if(hm.containsKey(al.get(index)))
{
count=hm.get(al.get(index));
hm.put(al.get(index),++count);
}
else
{
hm.put(al.get(index),1);
}
}
int ctr=0;
for(Map.Entry<String,Integer> m:hm.entrySet())
{
if(m.getValue()==n)
{
ctr++;
}
}
System.out.print(ctr);
}
}
input 1:
one two three four three five two
1
output 1:
3
S2 contained in S1 exactly at the same order or not:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
List<Integer> al=new ArrayList<>();
String str1=sc.next();
String str2=sc.next();
int l1=str1.length();
int l2=str2.length();
int ctr=0;
for(int index1=0;index1<l2;index1++)
{
for(int index2=ctr;index2<l1;index2++)
{
if(str2.charAt(index1)==str1.charAt(index2))
{
al.add(index2+1);
ctr=index2+1;
break;
}
}
}
int count=0;
for(int i=0;i<al.size()-1;i++)
{
if(al.get(i+1)>al.get(i))
{
count++;
}
}
if(count==l2-1)
System.out.print("YES");
else
System.out.print("NO");
}
}
input 1:
serksillnrvauegcsyek
skillrack
output 1:
YES
input 2:
geramtnts
men
output 2:
NO
Morse code conversion:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
while(sc.hasNext())
{
String str[]=sc.nextLine().split(" ");
int l=str.length;
StringBuilder sb=new StringBuilder();
for(int index=0;index<l;index++)
{
int len=str[index].length();
sb.append((char)(96+len));
}
System.out.print(sb.toString()+" ");
}
}
}
input 1:
111 1 11111111111111
1111111111111111111111111 111111111111111 111111111111111111111
11111111 11111 1 111111111111111111
1111111111111 11111
output 1:
can you hear me
find the correct operand:
import java.util.*;
public class Main
{
static char findOperator(int num1,int num2,int num3)
{
char ch='\0';
if(num1+num2==num3)
ch='+';
else if(num1-num2==num3)
ch='-';
else if(num1*num2==num3)
ch='*';
else if(num1/num2==num3)
ch='/';
return ch;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length(),j=0;
String str1="",str2="",str3="";
for(int i=0;i<l;i++)
{
if(!Character.isDigit(str.charAt(i)))
{
str1=str.substring(0,i);
for(j=i;j<l;j++)
{
if(str.charAt(j)=='=')
{
str2=str.substring(i+1,j);
break;
}
}
str3=str.substring(j+1,l);
break;
}
}
System.out.print(findOperator(Integer.valueOf(str1),Integer.valueOf(str2),Integer.valueOf(str3)));
}
}
input 1:
999+9=111
output 1:
/
input 2:
5+4=20
output 2:
*
print lower and uppercase in a string based on count:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
List<Character> upper=new ArrayList<>();
List<Character> lower=new ArrayList<>();
for(int index=0;index<l;index++)
{
if(Character.isUpperCase(str.charAt(index)))
upper.add(str.charAt(index));
else
lower.add(str.charAt(index));
}
if(lower.size()>upper.size())
{
for(Character c1:upper)
System.out.print(c1);
for(Character c2:lower)
System.out.print(c2);
}
else
{
for(Character c1:lower)
System.out.print(c1);
for(Character c2:upper)
System.out.print(c2);
}
}
}
input 1:
CodeJAmm
output 1:
CJAodemm
input 2:
SKILLRack
output 2:
ackSKILLR
String - words based on the same length with characters in alphabetical order:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String[] str=sc.nextLine().split(" ");
int l=str.length;
int ctr=0,flag=0;
List<Character> al=new LinkedList<>();
String[] output=new String[l];
for(int index=0;index<l;index++)
{
for(int index2=0;index2<str[index].length();index2++)
{
al.add(str[index].charAt(index2));
}
}
int size=al.size();
while(true)
{
for(int index=0;index<l;index++)
{
if(ctr<size&&output[index]==null)
{
output[index]=""+al.get(ctr++);
}
else if(ctr<size&&output[index].length()<str[index].length())
{
output[index]+=""+al.get(ctr++);
}
if(ctr>=size)
{
flag=1;
break;
}
}
if(flag==1)
break;
}
for(String s:output)
System.out.print(s+" ");
}
}
input 1:
a bc def ghij kl mnopq rs
output 1:
a bh cin djoq ek flprs gm
explanation:
the output string should contain same words as like other string. and it shoud contains characters in alphabetical order.
input 2:
a bc def ghij kl mnopq rs tuvw x yz
output 2:
a bk cls dmtw en fouxz gp hqvy i jr
Remove longest prefix and suffix in a string:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
int start=0,end=l-1,index=0;
while(start<end)
{
if(str.charAt(start)==str.charAt(end))
{
start++;
end--;
}
else
{
break;
}
}
String str1=str.substring(start,end+1);
if(str1==null)
System.out.print("-1");
else
System.out.print(str1);
}
}
input 1:
abcdefgdcba
output 1:
efg
input 2:
lkkl
output 2:
-1
Message shift encryption:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
int a=sc.nextInt();
int b=sc.nextInt();
int l=str.length();
int j=0;
for(int i=0;i<l;i++)
{
char c=str.charAt(i);
if(Character.isLetter(c))
{
if(c=='z')
{
c='a';
System.out.print((char)(c+a-1));
}
else
System.out.print((char)(c+a));
}
else if(Character.isDigit(c))
{
System.out.print(Character.getNumericValue(c)+b);
}
else
System.out.print(c);
}
}
}
input 1:
hello welcome to code jamm.2019##
2
3
output 1:
jgnnq ygneqog vq eqfg lcoo.53412##
reversed number sum:
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str1=sc.next();
String str2=sc.next();
StringBuilder sb1=new StringBuilder(str1);
StringBuilder sb2=new StringBuilder(str2);
int num=Integer.valueOf(sb1.reverse().toString())+Integer.valueOf(sb2.reverse().toString());
while(num!=0)
{
System.out.print(num%10);
num/=10;
}
}
}
input 1:
24
1
output 1:
34
explanation:
Reverse of 24 is 42 and 1 is 1.hence 42+1 = 43. reverse of that number is 34
input 2:
54
25
output:
79
print the characters in a string in reverse order:
import java.util.*;
public class Hello
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
Set<Character> st=new LinkedHashSet<>();
for(int index=0;index<l;index++)
{
st.add(str.charAt(index));
}
List<Character> al=new ArrayList<>(st);
Collections.sort(al,Collections.reverseOrder());
for(Character c:al)
System.out.print(c);
}
}
input 1:
cake
ouptut 1:
keca
input 2:
innovation
output 2:
vtonia
Integer range - count of subsets containing K:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
int y=sc.nextInt();
String s=sc.next();
long count=0;
for(int index=x;index<=y;index++)
{
String str=Integer.toString(index);
if(str.contains(s))
{
count++;
}
}
System.out.print(count);
}
}
input 1:
100 300 25
output 1:
12
input 2:
3000 6000 67
output 2:
67
Upper case - all alphabets present or not:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str1=sc.next();
String str2=sc.next();
StringBuilder sb=new StringBuilder(str1);
sb.append(str2);
String s=sb.toString();
int l=s.length();
int count=0;
for(int index=0;index<l;index++)
{
if(Character.isUpperCase(s.charAt(index)))
{
count++;
}
}
if(count==26)
System.out.print("yes");
else
System.out.print("no");
}
}
input 1:
ABCDEFGHIJKL
MNOPQRSTUVWXYZ
output 1:
yes
input 2:
ABcdefGHdADa
MNOPQsdddadASErHS
output 2:
no
Left and Right Half equal or not - after removing any character:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int flag=0;
for(int i=0;i<str.length();i++)
{
StringBuilder sb=new StringBuilder(str);
sb.deleteCharAt(i);
String s1=sb.substring(0,sb.length()/2);
String s2=sb.substring(sb.length()/2,sb.length());
if(s1.equals(s2))
{
System.out.print("YES");
break;
}
else
{
flag++;
}
}
if(flag==str.length())
System.out.print("NO");
}
}
input 1:
asdfsdf
output 1:
YES
input 2:
meome
output 2:
YES
input 3:
lione
output 3:
NO
sum of 2 and 3 digit integers in a string using REGEX:
import java.util.regex.*;
import java.util.*;
public class Main
{
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
List < String > al = new ArrayList <> ();
String str = sc.nextLine ();
Pattern p = Pattern.compile ("\\d+");
Matcher m = p.matcher (str);
while (m.find ())
{
al.add (m.group ());
}
long sum = 0;
int flag = 0;
for (int i = 0; i < al.size (); i++)
{
if (al.get (i).length () == 2 || al.get (i).length () == 3)
{
sum += Long.parseLong (al.get (i));
flag = 1;
}
}
if (flag == 0)
System.out.print ("-1");
else
System.out.print (sum);
}
}
input 1:
i have tim1 167 290 785:dsjd 46 fjh 34
output 1:
1322
Connection words or not:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int flag=0;
String[] str=sc.nextLine().split(" ");
int l=str.length;
for(int index=1;index<l;index++)
{
String s1=str[index-1];
String s2=str[index];
char c1=Character.toLowerCase(s1.charAt(0));
char c2=Character.toLowerCase(s2.charAt(s2.length()-1));
if(c1==c2)
{
flag++;
}
}
if(flag==l-1)
System.out.print("yes");
else
System.out.print("no");
}
}
input 1:
Apple Guaua Fig
output 1:
yes
explanation:
it is a connecting word if the first character of previous word is equal to the last character of the current word(ignoring cases)
input 2:
mango banana apple orange
output 2:
no
Number of groups formed by the same alphabets of the string:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
sc.nextLine();
String s[]=sc.nextLine().split(" ");
Set<String> set=new HashSet<>();
for(int i=0;i<a;i++){
char c[]=s[i].toCharArray();
Set<Character> t=new TreeSet<>();
for(Character j:c)
t.add(j);
String ss="";
for(Character j:t)
ss+=j;
set.add(ss);
}
System.out.println(set.size());
}
}
input 1:
6
tac market cat got act tog
output 1:
3
explanation:
three groups can be formed from the given strngs with same alphabets-- > (cat,tac,act) , (got,tog) , (market);
input 2:
4
a mack ace bark
output 2:
4
Count number of consecutive pairs repeated with same alphabets:
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
char[] str=sc.next().toCharArray();
int l=str.length;
int flag=0,count=0,index2=0;
for(int index1=0;index1<l-1;index1++)
{
flag=0;
if(str[index1]==str[index1+1])
{
for(index2=index1;index2<l-1;index2++)
{
if(str[index2]==str[index2+1])
flag++;
else
break;
}
if(flag>=1)
{
count++;
index1=index2;
}
}
}
System.out.print(count);
}
}
input 1:
aacdbbbcdba
output 1:
2
explanation:
there are two repeating set of alphabets aa,bbb.
input 2:
hhijkkkmnoooppp
ouptut 2:
4
input 3:
consecutive
output 3:
0
Top Scoring student:
import java.util.*;
public class Main
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String ans="";
int max=0;
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
String[] str=sc.next().split(":");
int sum=Integer.valueOf(str[1])+Integer.valueOf(str[2])+Integer.valueOf(str[3]);
if(sum>max)
{
max=sum;
ans=str[0];
}
}
System.out.print(ans);
}
}
input 1:
4
sasikumar:50:60:70
Arun:60:40:90
manoj:50:50:60
Rekha:60:35:45
output 1:
Arun
Smallest Possible number for a very long number sequence:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
List<Character> al=new ArrayList<>();
for(int i=0;i<str.length();i++)
al.add(str.charAt(i));
Collections.sort(al);
String s="";
char c='\0';
if(al.get(0)=='0')
{
for(int i=0;i<al.size();i++)
{
if(al.get(i)!='0')
{
c=al.get(i);
al.remove(i);
break;
}
}
s=s+c;
for(int i=0;i<al.size();i++)
{
s=s+al.get(i);
}
}
else
{
for(int i=0;i<al.size();i++)
{
s=s+al.get(i);
}
}
System.out.print(s);
}
}
input 1:
538642581265758181111111111111100000000000000048715431747164781899000000000838167481
output 1:
100000000000000000000000011111111111111111111122333444444555556666777777888888888899
input 2:
5386425812657581811111111111111487154317471647818997486857327582357897838167481
output 2:
1111111111111111111111222233333444444455555555666667777777777788888888888888999
Longest Common Substring Length:
import java.util.*;
public class Main
{
static int findLCS(String str1,String str2)
{
int l1=str1.length();
int l2=str2.length();
int[][] dp=new int[l1+1][l2+1];
int max=0;
for(int index1=1;index1<=l1;index1++)
{
for(int index2=1;index2<=l2;index2++)
{
if(str1.charAt(index1-1)==str2.charAt(index2-1))
{
dp[index1][index2]=dp[index1-1][index2-1]+1;
if(dp[index1][index2]>max)
{
max=dp[index1][index2];
}
}
}
}
return max;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str1=sc.next();
String str2=sc.next();
System.out.print(findLCS(str1,str2));
}
}
input 1:
nose
raise
output 1:
2
input 2:
fever
lever
output 2:
4
Length of longest substring with equal alphabets and digits:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
Map<Integer,Integer> hm=new HashMap<>();
int max=0,l=str.length(),count=0,position=0;
hm.put(count,position);
for(int index1=0;index1<l;index1++)
{
position++;
if(Character.isLetter(str.charAt(index1)))
{
count++;
}
else if(Character.isDigit(str.charAt(index1)))
{
count--;
}
if(hm.containsKey(count))
{
int currLen=position-hm.get(count);
if(currLen>max)
max=currLen;
}
else
hm.put(count,position);
}
System.out.print(max);
}
}
input 1:
abz23aacc1567
output 1:
12
input 2:
ab547b63
output 2:
6
All the alphabets are present or not (ignoring cases):
import java.util.*;
public class Main
{
static boolean checkAlphabet(String str)
{
int index=0,l=str.length();
boolean[] check=new boolean[26];
for(int i=0;i<l;i++)
{
if(Character.isUpperCase(str.charAt(i)))
index=str.charAt(i)-'A';
else if(Character.isLowerCase(str.charAt(i)))
index=str.charAt(i)-'a';
else
continue;
check[index]=true;
}
for(int i=0;i<26;i++)
{
if(check[i]==false)
return false;
}
return true;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str1=sc.nextLine();
String str2=sc.nextLine();
str1+=str2;
if(checkAlphabet(str1))
System.out.print("yes");
else
System.out.print("no");
}
}
input 1:
abdhfkkgjaksygasfoof
ifufDFDGfAEIHUGIAEYG
output 1:
no
input 2:
abcdEFGHiJK
LMNOpQQQRSTTTuuuvvvwwwwwZyxZ
output 2:
yes
Longest consecutive 1's length:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
String str=sc.next();
int l=str.length();
int len=0,max=0;
for(int index=0;index<l;index++)
{
if(str.charAt(index)=='0')
len=0;
else
len++;
max=Math.max(max,len);
}
if(max>1)
System.out.print(max);
else
System.out.print("-1");
}
}
input 1:
1001110110101
output 1:
3
input 2:
010101010001
output 2:
-1
Lexicographic sort of strings:
import java.util.*;
import java.util.stream.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
List<Character> al=new ArrayList<>();
int n=sc.nextInt();
String str=sc.next();
for(int index=0;index<n;index++)
{
al.add(str.charAt(index));
}
Collections.sort(al);
String newStr=al.stream().map(String::valueOf).collect(Collectors.joining());
System.out.print(newStr);
}
}
input 1:
8
codejamm
output 1:
acdejmmo
Sort strings in alphabetical order and print all positions:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
List<String> al=new ArrayList<>();
Set<String> st=new TreeSet<>();
int n=sc.nextInt(),pos=1;
for(int i=0;i<n;i++)
{
al.add(sc.next());
st.add(al.get(i));
}
List<String> al2=new ArrayList<>(st);
System.out.println(al2.size());
for(int i=0;i<al2.size();i++)
{
System.out.print(al2.get(i)+" ");
for(int j=0;j<al.size();j++)
{
if(al.get(j).equals(al2.get(i)))
System.out.print(j+1+" ");
}
System.out.println();
}
}
}
input 1:
7
abcd
some
abcd
efgh
abcd
some
efgh
output 1:
3
abcd 1 3 5
efgh 4 7
some 2 6