Komli Media Interview Question
SDETsCountry: India
Interview Type: Phone Interview
// No input validation
public class SringPermuatation {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
strPrnt("abcd", null, 0);
}
private static void strPrnt(String S, String Fix, int fixedPost) {
if (Fix == null) {
System.out.println(S);
Fix = "";
} else {
if(S.equals(Fix))
return;
System.out.println(Fix);
}
for (int i = fixedPost; i < S.length(); i++) {
strPrnt(S, Fix + S.charAt(i), i + 1);
}
}
}
In C++:
#include <iostream>
#include <sstream>
using namespace std;
void combinationsString(string word, string out, int start)
{
for(int i = start; i < word.length(); i++)
{
out += word.at(i);
cout << out << endl;
combinationsString(word, out, i + 1);
out.erase(out.length() - 1, 1);
}
}
int main(int argc, const char * argv[])
{
combinationsString("abc", "", 0);
//Output: a, ab, abc, ac, b, bc, c
}
public static void arrangeLettersToWordNoRepeat(char[] letters) {
int n = (int) Math.pow(2, letters.length);
for (int number = n - 1; number >= 0; number--) {
if (number == 0) {
System.out.print("empty string");
break;
}
for (int position = letters.length - 1; position >= 0; position--) {
if ((number & (1 << position)) != 0) {
System.out.print(letters[letters.length - 1 - position]);
}
}
System.out.println();
}
}
//Check my code i write Combinations function two ways use anyone
//My code is tested and compiled
#include<iostream>
using namespace std;
void PrintCombinations_other(char arr[],int i, int n, char result[], int j, int r)
{
if(r == 0)
{
result[j] = '\0';
cout<<result<<endl;
return;
}
if(i + r -1 >= n)
{
return;
}
for(int x = i; x + r - 1 < n; x++)
{
result[j] = arr[x];
PrintCombinations_other(arr,x+1,n,result,j+1,r-1);
while(arr[x] == arr[x+1])x++;
}
}
void PrintCombinations(char arr[],int i, int n, char result[], int j, int r)
{
if(j == r)
{
result[r] = '\0';
cout<<result<<endl;
return;
}
if(i == n)
{
return;
}
//include ith char or not include it
result[j] = arr[i];
PrintCombinations(arr,i+1,n,result,j+1,r);
//handle duplicates
while(arr[i] == arr[i+1])
i++;
//do not include ith char
PrintCombinations(arr,i+1,n,result,j,r);
}
void PrintAllLenStrings(char arr[])
{
int len = strlen(arr);
char* result = new char[len+1];
for(int i = len; i >= 1; --i)
{
PrintCombinations_other(arr,0,len,result,0,i);
}
}
int main()
{
char arr[] = "abcd";
PrintAllLenStrings(arr);
return 0;
}
Here is an implementation which uses only one recursion parameter. Also this does not include checking for duplicates before printing. That needs to be accounted for.
#include<stdio.h>
#include<string.h>
char in[100], s[100];
int N;
void combRec(int i);
void remSpacePrint();
void main()
{
scanf("%s",in);
N=strlen(in);
char s[100]={'\0'};
combRec(0);
}
void combRec(int i)
{
if(i==N)
{
remSpacePrint();
return;
}
s[i] = ' ';
s[i+1] = '\0';
combRec(i+1);
s[i]=in[i];
combRec(i+1);
}
void remSpacePrint()
{
char new[100];
int i=0,j=0;
while(i<=N)
{
if(s[i]==' ')
{
i++;
continue;
}
new[j++]=s[i++];
}
printf("%s\n", new);
}
#include <iostream.h>
#include <math.h>
#include<conio.h>
void combination(char * array, int size)
{
int n = pow(2, size);
for (int i = n; i >= 1; --i)
{
int k = i,j=0,p =size-1;
while (p >= 0)
{
k=k>>p;
if (k & 1)
{
cout<<array[j];
}
j++;
--p;
k=i;
}
cout<<endl;
}
}
void main()
{
char a[3] = {"abc"};
clrscr();
combination(a,3);
getch();
}
#include <iostream.h>
#include <math.h>
#include<conio.h>
void combination(char * array, int size)
{
int n = pow(2, size);
for (int i = n; i >= 1; --i)
{
int k = i,j=0,p =size-1;
while (p >= 0)
{
k=k>>p;
if (k & 1)
{
cout<<array[j];
}
j++;
--p;
k=i;
}
cout<<endl;
}
}
void main()
{
char a[3] = {"abc"};
clrscr();
combination(a,3);
getch();
}
I think we can start with an empty array, then for each new character, just add that character to all existing members of the empty array we had. we save quite a bit of space complexity here and duplications.
on algorithm complexity, It is o(n*m)
here is my objective c version, shame there is no better way to work with characters and strings...
NSString *string = @"abc";
NSMutableArray *characters = [NSMutableArray arrayWithCapacity:string.length];
[string enumerateSubstringsInRange:NSMakeRange(0, string.length)
options:(NSStringEnumerationByComposedCharacterSequences)
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[characters addObject:substring];
}];
NSMutableArray *output = [NSMutableArray arrayWithObject:@""];
[characters enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
NSArray *temporalOutput = [output copy];
[temporalOutput enumerateObjectsUsingBlock:^(NSString *newInput, NSUInteger idx, BOOL *stop) {
[output addObject:[newInput stringByAppendingString:obj]];
}];
}];
NSLog(@"output %@", output);
public class RecursionAnagrams1 {
private static void permutation(String prefix, String str){
int n = str.length();
System.out.println(prefix);
for (int i = 0; i < n; i++)
permutation(prefix + str.charAt(i),
str.substring(0, i) + str.substring(i+1));
}
public static void main(String[] args) {
permutation("", "ABC");
}
}
// ZoomBA
def all_comb_words( string ){
len = #|string|
words = fold ( [ 0: 2 ** len ] , set() ) -> {
s = str( $.o,2)
s = ('0' ** ( len - #|s| ) + s )
opt = fold ( s.value , '' ) -> {
$.p += ( $.o == _'1' ? string[ $.i ] : '' ) }
opt = str(opt) ; sorta(opt.value)
$.p += opt
}
println( words )
}
all_comb_words ( 'aabcc' )
Hey guys here order is matter because through this order only we will finalize our algotm
here the code are:
package com.sunil.carrercup;
public class String_Order {
/*
Given a String with print all the possible combinations of the all the characters in the string as a string for Example
"abc" is the input the you should print the below:
abc
ab
ac
a
bc
b
c
*/
public static void main(String[] args) {
String st="abcd";
char take;
System.out.println(st);
for(int i=0;i<st.length();i++){
take=st.charAt(i);
//System.out.println(take);
for(int j=i+1;j<st.length();j++){
System.out.println(take+""+st.charAt(j));
}
System.out.println(take);
}
}
}
it is like binary representation of 3 bits
- Anonymous July 05, 2014001
010
011
100
101
110
111
where ever 1 is there insert that character