Rushikesh Susar
Rushikesh Susar

@rushikesh_susar

9 Tweets 15 reads Jun 20, 2021
Day 1(#100daysofcode)String- A sequence of characters.
In java string is basically an object that represents sequence of char values. 3 important questions about string asked in interview - 1)Palindrome 2)check subsequence 3)check for anagrams
🧡Thread πŸ‘‡
Palindrome- String characters must be same if we read it from left to right or right to left
example1) I/P: str:"ABCDCBA"
here read from left to right:ABCDCBA
right to left :ABCDCBA same so it is a palindrome
O/P: Yes
2)I/P: str:"ABCA"
O/P:NO
function to check palindrome in java
boolean isPalindrome(String Str) {
int begin = 0;
int end = Str.length();
while(begin< end) {
if(Str.charAt(begin)!=Str.charAt(end)) {
return false; }
begin++;
end--; }
return true;
}
Check for subsequence- Example1)s1="ABC" s2="AC"
O/P:true (AC is subsequence of ABC)
{"A","B","C","AB","AC","BC","ABC"} all are subsequences of ABC
example2)s1="ABCD" s2="ADC"
O/P: FALSE(ADC is not a subsequence of ABCD)
function to check a subsequence in Java-
boolean isSubseq(String s1,String s2) {
int n = s1.length();
int m = s2.length();
int j=0;
for(int i=0;i<n&&j<m;i++)
{
if(s1.charAt(i) == s2.charAt(j)) {
j++;}
}return (j==m);
}
Check for anagrams-ex1)I/P: s1="listen" s2="silet"
O/P: true(s1 and s2 are anagrams)
ex2) I/P: s1="aabbc" s2="bbcaa"
O/P: true(we can see "a"-2 times in s1 and s2 "b"-2 times and "c"-1 time
ex3) I/P: s1:"abcb"s2:"baac"
O/P: false(s1 and s2 are not anagrams)
function to check Anagrams in Java-
static final int CHAR=256;
static boolean areAnagram2(String s1, String s2) {
if(s1.length()!=s2.length()) {return false;}
int [] count = new int [CHAR];
for(int i=0;i<s1.length();i++) {
count[s1.charAt(i)]++;
count[s2.charAt(i)]--;
}
for(int i=0;i<CHAR;i++) {
if(count[i]!=0)
{
return false;
}
}return true;
} (sorry angram function is written in two tweets due to limit in twitter tweet)#DEVCommunity #100daysofcodechallenge #placement
@rattibha unroll

Loading suggestions...