Add print all sequence of a string program (#30)
parent
1ff7ff348c
commit
e65d168ef6
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue