Problem
I have written a program to find out every possibility of Uppercase and Lowercase of a character for a given string.
An example would be, Input - "ab"/"Ab" etc. -- any one of those Output - ["ab","Ab","aB","AB"]
Code
Incorrect algorithm - please check below.
public static ArrayList<String> permuteUCLC(String a)
{
String s=new String(a.toLowerCase());
ArrayList<String> arr = new ArrayList<>();
arr.add(a);
int l = a.length();
for(int i=0;i<=l;i++)
{
for(int j=i+1;j<=l;j++)
{
arr.add(s.substring(0,i)+s.substring(i,j).toUpperCase()+s.substring(j,l));
}
}
Collections.sort(arr);
Collections.reverse(arr);
return arr;
}
Caution
I have realized after asking the question that my algorithm is wrong. I will try and upload correct algorithm in due course.
Subsequence Code (Correct Code) This is the code for finding all sub-sequences and upper-casing them. Assuming that all characters are unique. How to find the indices and implement it in functional way?
public static void permuteSubsequence(String a)
{
int n=a.length();
for(int i=0;i<(1<<n);i++)
{
String buff="";
for(int j=0;j<n;j++)
{
if(((1<<j)&i)!=0)
{
buff=buff+new Character(a.charAt(j)).toString().toUpperCase();
}
else
{
buff = buff + a.charAt(j);
}
}
System.out.println(buff);
}
}
Pick up the indices from the above case. i.e., the indices of 1's and uppercase them.
Request
How to convert the above code into functional style using Java streams?
The problem I am facing is simulating indices range in the map method.
Also, is there a way to generate Strings's Stream for copying same string into all elements, something similar to IntStream.range(a,b)
?
public static List<String> permuteStreams(String a)
{
int l=(int)(Math.pow(2,a.length())-1)
ArrayList<String> al = new ArrayList<>();
for(int i=0;i<=l;i++)
al.add(a);//Generate a stream and copy back into arraylist maybe if possible?
List<String> sl = al.stream()
.map()//incomplete code
.collect(Collectors.toList());
return sl;
}