Amazon Interview Question
Software Engineer / Developerspublic class PermutationOfStrings {
/**
*
* @param arr
*/
static void printArray(char arr[]) {
System.out.print("\n");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
}
/**
*
* @param i
* @param j
* @param arr
*/
static void permute(int i, int j, char arr[]) {
if (i == j) {
printArray(arr);
} else {
permute(i + 1, j, arr);
for (int z = i; z < j; z++) {
char temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
permute(i + 1, j, arr);
}
}
}
/**
*
* @param args
*/
public static void main(String[] args) {
permute(0, 3, new char[] { 'a', 'b', 'c', 'd' });
}
}
String ConcatenateStr(String str) {
return str +str;
}
String reverseString(String str){
return str.reverse();//reverse the string using any algo.
}
printPermutOfString(String str){
int sze= str/2;
for(int i=0;i<sze; i++){
int j=i;
Sysout(str.subStr(j,sze); //extracts substr from index j to length sze;
}
}
PSV main(String args[]){
String input = "abc";
String concStr = ConcatenateStr(input); //abcabc
printPermutOfString(concStr);//abc,bca,cab --output
String revStr = reverseStr(concStr);cbacba
printPermutOfString(revStr )//cba,bac,acb --ouput
}
public class StringCombinations {
public static void main (String[] args) {
StringCombinations s = new StringCombinations();
s.printStringCombinations("abcd".toCharArray(), 0);
}
private void printStringCombinations(char[] s, int level) {
if (level == s.length) {
System.out.println(s);
return;
}
for (int i = level; i < s.length; i++) {
swap(s, i, level);
printStringCombinations(s, level + 1);
swap(s, i, level);
}
}
private void swap (char[] s, int position1, int position2) {
char temp = s[position1];
s[position1] = s[position2];
s[position2] = temp;
}
}
Here I am assuming we can convert string to a array of characters and then proceed.
Length of a substring can be between (1 n), for any given length m, find all the combination of a binary array which contain m 1s and rest are all 0. and then print only those value of original array for which value in binay array is 1.
for example: input: 'abc', for length 1, binary array can be [100] [010] [001] == > print ==>a,b,c
for length 2, binary array [110][101][011] ==> ab, ac, bc
Here is working code
public class StringSubSet {
public static void PrintCombiOfGivenLength(char[] B, int[] Helper, int start, int remain){
if(remain == 0) {
System.out.print("[");
for (int i=0;i<Helper.length;i++){
if(Helper[i] == 1)System.out.print(B[i]+",");
}
System.out.println("]");
return;
}
else{
for(int i=start;i<B.length;i++){
Helper[i] = 1;
PrintCombiOfGivenLength(B,Helper,i+1,remain-1);
Helper[i] = 0;
}
}
}
public static void main(String[] args) {
char[] B = {'a','b','c'};
int[] Helper = new int[B.length];
for(int i=1; i<=B.length; i++) {
PrintCombiOfGivenLength(B,Helper,0,i);
}
}
}
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);
}
}
probably try to use factorial method to generate all possible strings from a given string. eg, string "abc" can be written as {abc,acb,bac,bca,cab,cba}..So, we got 3! number of words...this technique can be used along with rearranging the string
- T October 12, 2009