Add print all sequence of a string program (#30)

pull/31/head
Amisha Mohapatra 2021-01-26 17:35:13 +05:30 committed by GitHub
parent 1ff7ff348c
commit e65d168ef6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 0 deletions

View File

@ -3,6 +3,7 @@
### C or C++
1. [Palindrome Check](c-or-cpp/palindrome.c)
2. [All subsequences](c-or-cpp/sequence.cpp)
### C#
You could use any online IDE (for an example [.net Finddle](https://dotnetfiddle.net/)) to test them.
@ -16,3 +17,4 @@ You could use any online IDE (for an example [.net Finddle](https://dotnetfiddle
### Java
1. [Palindrome Check](java/palindrome.java)
2. [All subsequences](java/sequence.java)

View File

@ -0,0 +1,22 @@
#include<bits/stdc++.h>
using namespace std;
//pick and don't pick algorithm
//recrsion to print all subsequence
void permute(string t, int i, int n, string s){
if(i==n)
{
cout<<t<<endl;
}
else{
permute(t,i+1,n,s);
t = t + s[i];
permute(t,i+1,n,s);
}
}
int main(){
string s = "abc";
permute("", 0 , s.length() , s );
return 0;
}

View File

@ -0,0 +1,28 @@
import java.util.*;
class sequence {
static List<String> al = new ArrayList<>();
public static void main(String[] args)
{
String s = "abc";
findsubsequences(s, "");
System.out.println(al);
}
private static void findsubsequences(String s,
String ans)
{
if (s.length() == 0) {
al.add(ans);
return;
}
findsubsequences(s.substring(1), ans + s.charAt(0));
findsubsequences(s.substring(1), ans);
}
}