Ravi Ranjan
BAN USERHere is the Java code:
class reveseString{
public static void main(String args[])
{
String st = "yourString"; //Here you can replace yourString with some String
StringBuffer sbf = new StringBuffer(st);
int start=0,end=st.length()-1;
while(start<end)
{
sbf.setCharAt(start,(char)(sbf.charAt(start)^sbf.charAt(end)));
sbf.setCharAt(end,(char)(sbf.charAt(end)^sbf.charAt(start)));
sbf.setCharAt(start,(char)(sbf.charAt(start)^sbf.charAt(end)));
start++;
end--;
}
System.out.println(sbf);
}
}
We can do the permutation of the Characters.
here is the Java code:
import java.io.*;
public class StringPermutaion {
static StringBuffer result;
static int len=0;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
len = input.length();
result = new StringBuffer(input);
permute(0);
}
static void permute(int i){
if(i==len)
System.out.println(result);
else{
for(int j=i;j<len;j++)
{
swap(i,j);
permute(i+1);
swap(i,j);
}
}
}
static void swap(int i, int j){
char temp = result.charAt(i);
result.setCharAt(i, result.charAt(j));
result.setCharAt(j, temp);
}
}
For a string to become palindrome there are two cases:
1) if the length of the string is Odd. In this case we will have all the charaters even numbers of time except for one character.
2) if the length of the String is Even. here all the characters should occur even numbers of time.
So, we maintain a HashMap<Character,Integer> of the String
we iterate throught the map
and every entry in the map we check the value
if the length is even
if at anytime we found any key with value which is odd then return false(No Palindrome possible)
else if every key has even value then return true(palindrome is possible)
if the length is odd
if key has even value; continue;
if key has odd value keep a counter and increment its value for every odd value of a key.
if the counter!=1 return false;
else return true;
Sorting of word would itself take O(nlogn) where n is the length of the string.
Instead we can take 1st 26 prime number and allocate it to a-z. Now for every word we compute the product of the characters, this will be unique as we have chosen prime numbers.
Now for all the words Map it to HashMap<Integer,Word> if the key is there we have found one pair, else we insert the element into the map.
Create a StringBuffer and a set of characters.
We iterate throught the StringBuffer character by character and for every character we check the set.
if at any time we find that the character in StringBuffer is present in the set, we replace that character by a space(' ').
we carray on the above step till we reach the end of the StringBuffer.
Here is the Java code:
import java.util.*;
class Delimiter{
static StringBuffer sbf;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String st = sc.next();
sbf = new StringBuffer(st);
//create Set of the chars
//lets for simplicity I take chars as a d e k you can take it from user or anything
Set<Character> set = new HashSet<Character>();
set.add('a');
set.add('d');
set.add('e');
set.add('k');
for(int i=0;i<st.length();i++)
{
if(set.contains(sbf.charAt(i)))
sbf.setCharAt(i,' ');
}
System.out.println(sbf.toString());
}
}
Here we parse the 1st string("This is a boy") character by character, and everytime we check with the 1st char of the 2nd String("is" which is 'i' here). Now if we match with it then all we need to do is to compare substring till the size of 2nd string from the current index if the two string are equal if yes then we remove all the character from the 1st string from the current index to the size of the 2nd String.
if at any moment we find that the substring and the 2nd string are not equal the we move our cursor to the next character in the 1st striing and repeate the above steps.
And we can improve this by using finite automata.
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
static StringBuffer sbf = new StringBuffer("Hello");
public static void main (String[] args) throws java.lang.Exception
{
int high=sbf.length()-1;
int low =0;
while(low<high)
reverse((int)sbf.charAt(low),low++,(int)sbf.charAt(high),high--);
System.out.println(sbf.toString());
}
static void reverse(int a,int i,int b,int j)
{
a = a+b;
b = a-b;
a = a-b;
sbf.setCharAt(i,(char)a);
sbf.setCharAt(j,(char)b);
}
}
}
suppose I have a ransom money of 12345678 and magazine String contains "something 1 and blah blah 2 blan 8 ....." so my job is to find if I can get letter 1,2,3...8 out of the String.
I will create a HashMap of the Ransom money with key = the letter and value= number of time the letter has occured in the Rasom money.
Like in ransom money 255000. HashMap will look like {(2,1);(5,2);(0;3)}. This HashMap will have max size of 10 for each value of 0,1,2,3...9. (here instead of Map we can also use array of size 10);
Now I will I iterate throught the lenght of the Magazine article and for each letter I will look for the same in the HashMap.
if we find the match we decrement the count of the value of the key, and if the key has value =1 and match is found then we remove the Key from the map(this means that we got all the letter for that key)
we keep on doing the above step till the map is empty.
If the map.isEmpty()==true then return true;(ie it is possible to make the ransom amount)
else if we have reached the end of the magazine article but the map is not empty the retrun false;(we could not complete the ransom amount).
public boolean isRansomPossible(String ransom,String article)
{
int ransomLength = ransom.length();
int articleLength = article.length();
HashMap<Character,Integer> map = new HashMap();
for(int i=0;i<ransomLength;i++)
{
if(!map.containsKey(ransom.charAt(i)))
map.put(ransom.charAt(i),1);
else{
int value = map.get(ransom.charAt(i));
map.put(ransom.charAt(i),value+1)
}
}
for(int i=0;i<articleLength;i++)
{
if(map.isEmpty)
return true;
if(map.get(article.charAt(i)))
if(map.get(article.charAt(i)==1)
map.remove(article.charAt(i))
else
{
int value = map.get(article.charAt(i));
map.put(article.charAt(i),value-1);
}
}
return false;
}
Supposing that we have a dictonary to check if the word form is valid we can permute the letters and for each permutation we can lookup in the dictonary if the word is valid if yes then we can suggest that word.
StringBuffer result = new StringBuffer("miss_spelled_word");
void permute(int i){
if(i==len)
System.out.println(result);
else{
for(int j=i;j<len;j++)
{
swap(i,j);
permute(i+1);
swap(i,j);
}
}
}
static void swap(int i, int j){
char temp = result.charAt(i);
result.setCharAt(i, result.charAt(j));
result.setCharAt(j, temp);
}
import java.util.Scanner;
public class DateConverter {
public static void main(String agrs[])
{
Scanner sc = new Scanner(System.in);
int date = sc.nextInt();
System.out.println(getDate(date));
}
static String getDate(int d)
{
String[] arr = {"Jan","Feb","March","April","May","June","July","Aug","Sept","Oct","Nov","Dec"};
int year = d%100;
d/=100;
int month = d%100;
d/=100;
int date = d%100;
if(year>=00 && year<=15)
year=2000+year;
else
year = 1900+year;
return (date+" "+arr[month-1]+" "+year);
}
}
I have used the fact that if year is between 00-15 then it is 2000 else we are talking about the 1900.
Here is Java code
- Ravi Ranjan March 08, 2015class LongestConsecutive{
public static void main(String args[])
{
String st = "abbcccdddd";
StringBuffer sbf = new StringBuffer(st);
int max = 1,len=st.length(),count=1,index=0,startIndex=0;
for(int i=1;i<len;i++)
{
if(sbf.charAt(i)==sbf.charAt(index))
{
count++;
}
else{
if(count>max)
{
max = count;
startIndex = index;
}
index = i;
count = 1;
}
}
if(count>max)
{
max = count;
startIndex=len-max;
}
System.out.println(sbf.substring(startIndex,startIndex+max).toString()+" "+max);
}
}