razik.islam
BAN USERHere is the solution using recursion. I think it is very easy to understand. feel free to share any pros and cons of the code.
public class longestpalindrome {
List<String> list = new ArrayList<String>();
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="ABCDCBAUVUCBALKJKLABC";
longestpalindrome obj = new longestpalindrome();
obj.maxpalindrome(s);
int max=1;
String s2="";
for(String s1 : obj.list){
if(s1.length()>max){
max=s1.length();
s2=s1;
}
}
if(s2.isEmpty())
System.out.println("No palindrome available");
else
System.out.println("Longest palindrome is "+s2);
}
void maxpalindrome(String s){
longestpalindrome obj1 = new longestpalindrome();
if(obj1.checkpalindrome(s) && s.length()>1){
list.add(s);
}
else{
if(s.length()>1){
maxpalindrome(s.substring(1));
maxpalindrome(s.substring(0,s.length()-1));
}
}
}
boolean checkpalindrome(String s){
int i=0;
int j=s.length()-1;
while(i<=j){
if(s.charAt(i)==s.charAt(j)){
i++;
j--;
}
else
return false;
}
return true;
}
}
Don't get confused with the char c. it is redundant, just ignore the use of it.
- razik.islam February 01, 2014I have got an easy and basic solution for this.
public static void main(String[] args) {
String s="I Love You and You Love Me";
s=" "+s;
String s1="";
int l=s.length()-1;
int ei=0;
while(l>=0){
if(s.charAt(l)==' '){
s1=s1+s.substring(l+1, ei+l+1)+" ";
l--;
ei=0;
}
else{
l--;
ei++;
}
}
char c ='A';
System.out.println("Output string is "+s1+" and ascii of A is "+(int)c);
}
I have got to solve it in O(N). Please let me know if you think this will not work in any scenario.
public static void main(String[] args) {
String s1="abfghifghijcdehuj";
String s2="fghij";
isStringContainsSubstring obj = new isStringContainsSubstring();
int index = obj.findIndexOfSubsting(s1,s2);
System.out.println("Starting index of substring is "+index);
}
int findIndexOfSubsting(String s1,String s2){
int i=0,j=0;
while(i<s1.length()){
if(s1.charAt(i)==s2.charAt(j)){
i++;
j++;
if(j==s2.length()){
return i-j;
}
}
else{
if(j==0)
i++;
j=0;
}
}
return -1;
}
I have used a very basic approach. Hope it will help you guys.
public static void main(String[] args) {
isPalindromeSubstring isp = new isPalindromeSubstring();
String s="abcdefghijklmnbb";
isp.functionPalindrome(s);
if(isp.done==false)
System.out.println("No aplindrome substring");
}
public void functionPalindrome(String s){
if(checkPalindrome(s) && s.length()>=2 && done==false){
done=true;
System.out.println("String contains palindrome which is "+s);
return;
}
else{
if(s.length()>=2){
functionPalindrome(s.substring(1));
functionPalindrome(s.substring(0, s.length()-1));
}
}
}
boolean checkPalindrome(String s){
int j=s.length()-1;
for(int i=0;i<=j;i++){
if(s.charAt(i)==s.charAt(j))
j--;
else{
return false;
}
}
return true;
}
O(n) time Complexity
public class findSubstingSyncWithmap {
/**
* @param args
*/
public static void main(String[] args) {
String s="acbadrbdady";
Map m = new HashMap();
boolean done=false;
m.put('b', 1);
m.put('a', 1);
m.put('d', 2);
int si=0;
Set<Character> set = new HashSet<Character>();
set=m.keySet();
Map<Character,Integer> mcopy = new HashMap<Character,Integer>();
mcopy.putAll(m);
for(int i=0;i<s.length();i++){
if(set.contains(s.charAt(i))){
if(done==false)
si=i;
done=true;
int k=(int)mcopy.get(s.charAt(i));
mcopy.put(s.charAt(i), k-1);
}
else{
Iterator<Integer> it = mcopy.values().iterator();
while(it.hasNext()){
if(it.next()!=0){
done=false;
mcopy.clear();
mcopy.putAll(m);
break;
}
}
if(done==true){
System.out.println("I have got the substring which is "+s.substring(si, i));
}
}
}
}
}
I have used a very basic approach which makes the program very simple.
Here are the steps :-
Step 1 :- consider numbers starts with one digit. so, i have passed j=1 along with the string.
Step 2:- traverse through the string and when encounter any number which gives greater length of number after adding 1 ( eg:- 9,99,999,.....) increase j by 1.
Step 3:- if the next number of j length is greater by 2 than current number then current number+1 is missing.
Step 4:- if next number is continuous then continue the traversing.
Step 5:- if next number is neither greater by 2 nor by 1 then you are traversing the string in wrong way considering length j. In that case call the function again and assume the length of the number starts with j++.
step 6:- continue with step 2.
Assumptions:- there are no duplicate numbers and only 1 number is missing.
- razik.islam February 08, 2014