Nikhilesh
BAN USER
package Strings;
import java.util.ArrayList;
public class AnagramsOfLettersOnly {
public static void main(String[] args) {
String input = "Hello";
StringBuilder indexesOfLetters = new StringBuilder();
for(int i = 0; i < input.length(); i++) {
if(input.charAt(i) > 'a' && input.charAt(i) < 'z')
indexesOfLetters.append(i);
}
if(indexesOfLetters.length() == 0 || indexesOfLetters.length() == 1) {
System.out.println("No anagrams for this string");
return;
}
ArrayList<String> permutationsOfIndexes = getPermutations(indexesOfLetters.toString());
for(String eachPermutation: permutationsOfIndexes) {
StringBuilder builder = new StringBuilder(input);
for(int i = 0; i < eachPermutation.length(); i++) {
builder.setCharAt(Character.getNumericValue(indexesOfLetters.charAt(i)), input.charAt(Character.getNumericValue(eachPermutation.charAt(i))));
}
System.out.println(builder);
}
}
public static ArrayList<String> getPermutations(String str) {
ArrayList<String> permutations = new ArrayList<String>();
if(str == null)
return null;
if(str.length() == 0) {
permutations.add(str);
return permutations;
}
char currentChar = str.charAt(0);
String remainingString = str.substring(1);
ArrayList<String> words = getPermutations(remainingString);
for(String word : words) {
for(int i = 0; i <= word.length(); i++) {
String newWord = addCharAt(word, i, currentChar);
permutations.add(newWord);
}
}
return permutations;
}
public static String addCharAt(String word, int i, char currentChar) {
return word.substring(0, i) + currentChar + word.substring(i, word.length());
}
}
package Companies.Expedia.Strings;
public class FindNextBigNumber {
public static void main(String[] args) {
String str = "871265";
int index = checkIfPossible(str);
if(index == -1) {
System.out.println("This is the biggest number");
return;
}
else {
str = calculateNextBig(str, index);
System.out.println(str);
}
}
public static int checkIfPossible(String str) {
int index = -1;
for(int i = str.length() - 1; i > 0; i--) {
if(str.charAt(i) > str.charAt(i - 1)) {
index = i - 1;
return index;
}
}
return index;
}
public static String calculateNextBig(String str, int index) {
int nextHighestIndex = index + 1;
for(int i = nextHighestIndex + 1; i < str.length(); i++) {
if((Character.getNumericValue(str.charAt(i)) < Character.getNumericValue(str.charAt(nextHighestIndex))) &&
(Character.getNumericValue(str.charAt(i)) > Character.getNumericValue(str.charAt(index)))) {
nextHighestIndex = i;
}
}
str = swap(str, index, nextHighestIndex);
str = reverseRemaining(str, index + 1);
return str;
}
public static String swap(String str, int index, int nextHighestIndex) {
StringBuilder s = new StringBuilder(str);
char temp = s.charAt(index);
s.setCharAt(index, s.charAt(nextHighestIndex));
s.setCharAt(nextHighestIndex, temp);
return s.toString();
}
public static String reverseRemaining(String str, int index) {
int start = index;
int end = str.length() - 1;
while(start < end) {
str = swap(str, start, end);
start++;
end--;
}
return str;
}
}
import java.util.LinkedList;
public class EvaluateLeftToRight {
static LinkedList<Integer> numQueue = new LinkedList<Integer>();
static LinkedList<String> opQueue = new LinkedList<String>();
public static void main(String[] args) {
String expression = "3*4+5-9+6";
if(expression.length() == 0)
System.out.println("Empty String");
if(expression.length() == 1)
System.out.println(Integer.parseInt(expression));
splitExpression(expression); //method to split the input string into numbers and operators
int result = evaluateExpression(); //method to evaluate the expression
System.out.println(result);
}
public static void splitExpression(String expression) {
String number = "";
for(int i = 0; i < expression.length() - 1; i++) {
// Check if character is operator or digit, if it is a operator, add the operator to the queue
if(expression.charAt(i) == '-' || expression.charAt(i) == '+' || expression.charAt(i) == '*')
opQueue.add(String.valueOf(expression.charAt(i)));
// if the character is a digit
else {
number += expression.charAt(i);
//then check for the next character, if it is an operator, the number ends here
if(expression.charAt(i + 1) == '-' || expression.charAt(i + 1) == '+' || expression.charAt(i + 1) == '*') {
numQueue.add(Integer.parseInt(number));
number = "";
}
}
}
int i = expression.length();
// The last position in the string is yet to be operated on
// Find the second last position, if it is an operator, then the character at last position is a single digit number
if(expression.charAt(i - 2) == '-' || expression.charAt(i - 2) == '+' || expression.charAt(i - 2) == '*')
numQueue.add(Integer.parseInt(expression.substring(i - 1)));
// Else, the number might contain more than one digit
else {
number += expression.charAt(i - 1);
numQueue.add(Integer.parseInt(number));
}
}
public static int evaluateExpression() {
int number1 = numQueue.poll();
while(!opQueue.isEmpty()) {
int number2 = numQueue.poll();
char operator = opQueue.poll().charAt(0);
switch(operator) {
case '+' : number1 = number1 + number2; break;
case '-' : number1 = number1 - number2; break;
case '*' : number1 = number1 * number2; break;
}
}
return number1;
}
}
Repjuanitajboon, Applications Developer at 247quickbookshelp
Hi everyone, I am from Pelham. I currently work in the Hechinger as Cashier.I like to do creative things ...
Repsonjamramos45, Graphics Programmer at CGI-AMS
I am a strong writer with a passion for story-telling who has extensive experience of writing literary compositions, articles, reports ...
Repjenniferdray9, Accountant at ABC TECH SUPPORT
Hi I am Jennifer D. Ray from san Diego.Currently i am working as a parts salesperson in Rite solution ...
Repmartinskrull, Analyst at A9
Hi everyone, I am from new york,USA. I currently work in the Affiliate Marketing industry. I love all things ...
Repman254183, Project Leader at GoDaddy
I am working as Human Resources Associates, and my duties are for obtaining, recording, and interpreting human resources information within ...
RepAmberBrook, Animator at A9
Hi everyone, Done my master of arts in specialized journalism.Also an member of society of professional journalists since 2015 ...
Repwaynebgrover, AT&T Customer service email at ASAPInfosystemsPvtLtd
I am 31 years old and live in San Jose with my family. I have all types of books and ...
RepDonnaArvin, Analyst at Apple
Hii am from the United States. I work as a computer control programmer at Dynatronics Accessories. I am a social ...
RepAvikaEthan, Data Engineer at Adjetter Media Network Pvt Ltd.
Avika Ethan has five years of experience collecting physical and biological data in California streams and rivers. In the field ...
- Nikhilesh April 09, 2015