Learn-dsa..in 30 days!



























CC-6 :Find all unique sub sequences that can be formed from characters of a String.

Description:

Given an input String str, find and return all unique subsequences that can be formed by the characters of str.

Test cases and expected outputs:

Input Parameters Expected outputs
str=”sugar” Subsequences: : a,su,sug,g,suga,uga,ar,ug,r,s,u,ugar,gar,ga,sugar
str=”morning” Subsequences: : ornin,ning,mor,mornin,nin,rnin,rning,mo,or,ing,in,g,i,orn,orni,rni,m,morn,n, morning ,o,r,orning,ng,ni,rn,morni

Pseudocode:

The java method should accept following input parameters: str (String).
Initialize a variable set of type HashSet to hold the unique subsequences that can be formed from str.
Using a for loop iterate through str, using idx1 as a loop variable with initial value 0 and increment idx1 till it reaches str.length()-1:
Using a for loop iterate through str, using idx2 as a loop variable with initial value idx1+1 and increment idx2 till it reaches str.length()-1:
Using String.substring() method, extract a substring from str with start index idx1 and end index str2.
Add the extracted substring to set.
At completion of above loop, set contains all the unique subsequences of str.
Use an iterator to access elements from set and add the same to String array named retSubArr. Return retSubArr.

Code:

public boolean setArraysCheckDisjoint(String[] arr1, String[] arr2) {
	HashSet<String> set1=new HashSet<String>();
	for (int idx=0; idx < arr1.length; idx++) {
		set1.add(arr1[idx]);
	}
	for (int idx=0; idx < arr2.length; idx++) {
		if(set1.contains(arr2[idx])) {
			return false;
		}
	}
	return true;
}

Click here to download and run code and test cases !