Trending December 2023 # Print All Subsequences Of A String In C++ # Suggested January 2024 # Top 19 Popular

You are reading the article Print All Subsequences Of A String In C++ updated in December 2023 on the website Bellydancehcm.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 Print All Subsequences Of A String In C++

In this problem, we are given a string and we have to print all the subsequences of the string. The substring generated is created by deleting the elements of the string but the order remains the same(i.e. Order cannot be changed).

Let’s take an example to understand the topic better −

Input: xyz Output: x,y,z,xy,yz,xz,xyz

Explanation − In the above example, we can see the only characters are deleted to create substring. No, rearranging takes place.

There can be multiple methods to solve this problem, here we will discuss a few of them to understand methods.

One is by selecting elements of the string and eliminating a few to create a sequence. In this method, we will pick a few elements and delete the rest to create the substring.

Example import java.util.*; class Main{    public static void main(String[] args) {       String s="pqrs";       System.out.println("All the substring found are :");       findSubString(s,"");       System.out.println(subStringSeq);    }    public static void findSubString(String s, String ans) {       if(s.length()==0){          subStringSeq.add(ans);          return;       }       findSubString(s.substring(1),ans+s.charAt(0)) ;       findSubString(s.substring(1),ans);    } } Output

All the substring found are −

[pqrs, pqr, pqs, pq, prs, pr, ps, p, qrs, qr, qs, q, rs, r, s, ]

Another method could be iterating over the string and generate substring. And dropping characters of the sequence to generate substrings. Here, we will use a list to store the substrings. And check if the sequence found is already found or not.

Example import java.util.HashSet; public class Main{    static void findSubString(String str){       for (int i = 0; i < str.length(); i++) {             String sub_str = str.substring(i, j);             if (!subString.contains(sub_str))                subString.add(sub_str);             for (int k = 1; k < sub_str.length() - 1; k++) {                StringBuffer sb = new StringBuffer(sub_str);                sb.deleteCharAt(k);                if (!subString.contains(sb));                   findSubString(sb.toString());             }          }       }    }    public static void main(String[] args){       String s = "pqrs";       System.out.println("The subsequence is ");       findSubString(s);       System.out.println(subString);    } } Output

The subsequence is

[rs, pq, qr, pr, qs, ps, prs, p, pqr, q, r, s, pqs, qrs, pqrs]

One more method can be fix characters and find substring. In this method we will fix elements of the string one by one and using these fixed characters, we will find the subsequence. Recursive calling of this method creates the required string subsequence.

Example class Main {    static void subString(String str, int n,    int index, String curr){       if (index == n){          return;       }       System.out.print(curr + ", ");       for (int i = index + 1; i < n; i++){          curr += str.charAt(i);          subString(str, n, i, curr);          curr = curr.substring(0, curr.length() - 1);       }    }    static void printSubStrings(String str){       int index = -1;       String curr = "";       subString(str, str.length(), index, curr);    }    public static void main(String[] args){       String str = "pqrs";       System.out.println("The subStrings are :") ;       printSubStrings(str);    } } Output

The subStrings are −

p, pq, pqr, pqrs, pqs, pr, prs, ps, q, qr, qrs, qs, r, rs, s

You're reading Print All Subsequences Of A String In C++

Working And Examples Of Composition In C#

Introduction to Composition C#

Web development, programming languages, Software testing & others

In this type of relationship, one or greater than one object of a different class is declared in the related class. Here come two more divisions, which are aggregation and composition. In aggregation, the nested objects can independently exist in the class without being an integral part of the class. On the other hand, in composition, the nested objects or a singular nested object supplements the class, which makes the class inconceivable without their or its existence.

Syntax of Composition in C#

Given below is the syntax mentioned:

class Training { } public class Course { Project project = new Project(); } Working of Composition in C#

Composition in C# is a way of creating a relationship between two classes that one or greater than one nested objects are a part of the related class, and the logical existence of class becomes impossible without the nested objects.

For example, if we consider a class called Car, then it should have one instance of class “Engine.” Moreover, it should also have four other instances of the class “Wheel.”

Now, if we eliminate any of these instances, then the Car won’t function.

Examples of Composition C#

Given below are the examples of Composition C#:

Example #1

If the training class is considered, which is describing two courses. Now, the courses are being used for describing the course class. Therefore, the training class cannot exist without the two-course instances as both of these instances are part of the course class. Moreover, both of these instances of the course class are also a part of the training class.

Code:

using System; using static System.Console; namespace EDUCBA { class Course { public double M; public double A; } class Training { public Course course1 = null; public Course course2 = null; } class Career { public Course[] courses; public Training[] trainings; public Career() { courses = null; trainings = null; } public void Print() { WriteLine(" Courses Data is represented below:"); for (int b = 1; b< courses.Length; b++) { WriteLine("n M = {0}, A = {1}", courses[b].M, courses[b].A); } WriteLine("n Trainings Data is represented below:"); for (int b=1; b<trainings.Length; b++) { WriteLine("n course1.M = {0}, course1.A = {1}", trainings[b].course1.M, trainings[b].course1.A); WriteLine("n course2.M = {0}, course2.A = {1}", trainings[b].course2.M, trainings[b].course2.A); } } } class Code { static void Main(string[] args) { Career O = new Career(); O.courses = new Course[9]; for (int b = 1; b < O.courses.Length; b++) { O.courses[b] = new Course(); O.courses[b].M = b * b; O.courses[b].M = b * b * b; } O.trainings = new Training[5]; for (int b = 1; b < O.trainings.Length; b++) { O.trainings[b] = new Training(); O.trainings[b].course1 = new Course(); O.trainings[b].course2 = new Course(); O.trainings[b].course1.M = b; O.trainings[b].course1.A = b * 4; O.trainings[b].course2.M = b * 5; O.trainings[b].course2.A = b * b; } O.Print(); } } }

Output:

Example #2

In this example, both of the classes created are regular classes; however, the course class is using an instance from the project class inside it. This is the same way in which one function is called inside another. Using inheritance, we can have access to each and everything from the Project class. However, using composition, only the code specified by us can be accessed. Here, we can access the Project class indirectly.

Code:

using System; namespace EDUCBA { class Training { static void Main(string[] args) { Course courses = new Course(); courses.Bought(); Console.ReadLine(); } } public class Project { public void Log(string aboutus) { Console.WriteLine(aboutus); } } public class Course { Project project = new Project(); public void Bought() { } } }

Output:

Example #3

Code:

Output:

Conclusion

On the basis of the above article, we understood the concept of composition in C#. We went through multiple examples for understanding the application of composition in C# coding.

Recommended Articles

This is a guide to Composition C#. Here we discuss the introduction, working of composition in C#, and examples, respectively. You may also have a look at the following articles to learn more –

Working Of Predicate Delegate In C# With Examples

Introduction to C# Predicate

Web development, programming languages, Software testing & others

Syntax:

Where the object type is represented by P and obj is the object which compares the criteria that is defined within a method and is  represented by predicate delegate.

Working of Predicate Delegate in C#

A function that returns true or false is a predicate and the reference to a predicate is a predicate delegate.

The feature of predicate delegate was introduced with the release of .NET 2.0. framework.

The predicate function can be defined, and it can be passed as a parameter to any other function through predicate delegate.

A special case of Func is predicate delegate which takes only one parameter as input and it returns a Boolean value that is either true of false.

Any method can be written inside a predicate delegate even the expression of lambda or anonymous method.

A generic type is taken as an argument by the predicate delegate when it is used with the expression of lambda.

Examples of C# Predicate

Given below are the examples mentioned:

Example #1

C# program to demonstrate the use of predicate delegate in a program to check if the given string passed as a parameter is in capital letters or not.

Code:

using System; namespace program { public class check { static bool IsUC(string stri) { return stri.Equals(stri.ToUpper()); } static void Main(string[] args) { bool res = isU("welcome to c#"); Console.WriteLine(res); } } }

Explanation:

In the above program, a namespace called program is defined. Then a class called check is defined. Then a Boolean method is defined to check if the given string is written in capital letters or not. If the given string is written in capital letters, true is returned else False is returned. Then the main method is called.

Then a predicate delegate is defined with object type as string and IsUC is an object which compares the criteria that is defined within a method and is represented by predicate delegate. Then the result of the predicate delegate is stored in a variable called res. Then the result is displayed.

Example #2

C# program to demonstrate the use of predicate delegate in a program to check if the length of the given string is less than a specified value or not.

using System; class program { public delegate bool my_del(string stri); public static bool fun(string stri) { if (stri.Length < 5) { return true; } else { return false; } } static public void Main() { my_del obj = fun; Console.WriteLine(obj("Shobha")); } }

Output:

Explanation:

In the above program, a class called program is defined. Then a predicate delegate is defined with object type as string. Then a method is defined inside a predicate delegate by passing the object as parameter to check if the length of the given string is less than a specified value. If the length of the string is less than the given specified value, true is returned else false is returned.

Then the Main method is called. Then a predicate delegate is defined with object type as string and fun is an object which compares the criteria that is defined within a method and is represented by predicate delegate. Then the string to be passed as a parameter to predicate delegate is written at last.

The Predicate delegates are useful when we have to filter out a list of values.

The predicate delegates can be made inline for one off search function.

The predicate delegates can be used when we have to search for items in a generic collection.

By using predicate delegates, the length of the code is shortened and either true or false is returned.

Anonymous methods, expression of lambda can be assigned to predicate delegates.

The predicate delegates provide the logic at runtime and it can be a simple logic or complicated logic.

Recommended Articles

Minimum Number Of Adjacent Swaps To Convert A String Into Its Given Anagram

Every string is formed by a sequence of characters arranged in an order. The string may be composed of letters, number or even special characters.

An anagram of any input string, is the string with a random permutation of characters. This implies, that when the order of the characters is rearranged, an anagram of the string is obtained. The respective counts of the characters should also remain the same in anagrams. Two anagram strings have the following implications −

Both of them contains the same set of characters.

Both of them may have a different permutation of characters

For instance, feed and def are not anagrams of each other, since the character ‘e’ in the first string is repeated twice.

Adjacent characters are the neighbouring positioned letters in a string.

In order to equalise the strings, the corresponding characters of both the strings have to be matched.

The problem statement is to first check whether the given two strings are anagram of each other and return the minimum of swaps required to obtain equal anagram strings, if they are.

Some of the examples illustrating the problem statement are as follows −

Sample Example

Example 1 – str1 : “abca”

str2 : “baac”

Output : 2

Explanation : The following strings are anagrams of each other, and the str1 can be converted to str2 by performing the following two swaps −

swap(0,1) which yields “baca”

swap(2,3) which yields “baac”

Example 2 – str1 : “aaf”

str2 : “faa”

Explanation : 2

Initially, the swap(2,3) is performed to yield “afa”

Then, swap(1,2) is performed to yield “faa”

This problem can be solved by using the concepts of character checking and using STL inbuilt-methods −

sort() to sort the string

swap(i,j) to swap the characters at the i and jth index positions respectively.

Algorithm

Step 1 − The provided two strings, str1 and str2 are sorted using the sort() method.

Step 2 − Their characters are respectively compared to check if the two strings are equivalent in nature.

Step 3 −If the two sorted strings are equivalent then they are anagram of each other.

Step 4 −A counter is maintained to keep a track on the number of swaps performed until now.

Step 5 − Two pointers, i and j are initialised and the pointer owing to the second string is incremented until the character at the ith index of the first string and jth index of second string match, such that str1[i] = str2[j].

Step 6 − In order to match the positions of equal characters, the adjacent elements and the indices j and j-1 are interchanged using the swap() function.

Step 7 − The j counter is then decremented till it becomes equivalent to i.

Step 8 − The i pointer is now incremented.

Step 9 − This procedure follows until the entire length is exhausted.

Example

The following C++ code snippet takes as input two strings, and finds out if these two strings are anagrams of each other and the least number of swaps to convert them to equivalent strings

using namespace std; bool checkanagram(string s1, string s2) { sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); if (s1 == s2) return true; else return false; } int minSwaps(string str1, string str2) { int i = 0, j = 0; int minswaps = 0; int len = str1.length(); for(i=0; i < len;i++) { j = i; while (str1[j] != str2[i]) { j += 1; } while (i < j) { swap(str1[j],str1[j-1]); minswaps += 1; j--; } } return minswaps; } int main() { string str1 = "male"; string str2 = "lame"; bool anares = checkanagram(str1,str2); if(anares){ int swaps = minSwaps(str1,str2); cout<<"Minimum no of swaps to convert string1 to string2 : "<<swaps; } else cout << "Strings are not anagram of each other."; return 0; } Output Minimum no of swaps to convert string1 to string2 : 3 Conclusion

The anagram strings are just different permutations of the same set of characters. They can be made similar by just swapping the characters at the corresponding positions.

The time complexity of the above approach is O(n*n), where n is the length of the string. This is because each index character in the first string is searched for in the entire length of second string.

The space complexity of the above specified algorithm is O(1), which is constant in nature.

C++ Program To Implement Double Order Traversal Of A Binary Tree

Here is a C++ Program to Implement Double Order Traversal of a Binary Tree.

In Double Order Traversal, the root of the subtree is will be traversed twice.

Golang Program To Check If A String Contains A Substring

A substring is a small string in a string and string in Golang is a collection of characters. Since strings in Go are immutable, they cannot be modified after they have been produced. Concatenating or adding to an existing string, however, enables the creation of new strings. A built-in type in Go, the string type can be used in a variety of ways much like any other data type.

Syntax strings.Contains(str,substring string)

To determine whether a string contains a particular substring, use the Contains(s, substr string) bool function. If the substring is found in the supplied string, a boolean value indicating its presence is returned.

strings.Index(str, substring string)

The int function index(s, str string) is used to determine the index of the first instance of a specified substring within a given string. It returns either -1 if the substring is missing or the index of the substring within the string.

strings.Index(str, substring string)

The int function index(s, str string) is used to determine the index of the first instance of a specified substring within a given string. It returns either -1 if the substring is missing or the index of the substring within the string.

Algorithm

Step 1 − Create a package main and declare fmt(format package) and strings package

Step 2 − Create a function main and in that function create a string mystr

Step  3 − Using the string function, check whether the string contains the substring or not

Step 4 − Print the output

Example 1

In this example we will see how to check if a string contains a substring using a built-in function strings.Contains(). The output will be a Boolean value printed on the console. Let’s see through the code and algorithm to get the concept easily.

package main import ( "fmt" "strings" ) func main() { mystr := "Hello,alexa!" fmt.Println("The string created here is:", mystr) substring := "alexa" fmt.Println("The substring from the string is:", substring) fmt.Println("Whether the substring is present in string or not?") fmt.Println(strings.Contains(mystr, substring)) } Output The string created here is: Hello,alexa! The substring from the string is: alexa Whether the substring is present in string or not? true Example 2

In this example, we will see how to check if a string contains a substring or not using strings.Index() function.

package main import ( "fmt" "strings" ) func main() { mystr := "Hello, alexa!" fmt.Println("The string created here is:", mystr) substring := "alexa" fmt.Println("The substring from the string is:", substring) fmt.Println("Whether the string contains the substring or not?") fmt.Println("The string contains the substring.") } else { fmt.Println("The string does not contain the substring.") } } Output The string created here is: Hello, alexa! The substring from the string is: alexa Whether the string contains the substring or not? The string contains the substring. Example 3

In this example we will see how to find if a string contains a substring using for loop in strings.Index() function −

package main import ( "fmt" "strings" ) func main() { mystr := "Hello, alexa!" fmt.Println("The string created here is:", mystr) substring := "alexa" fmt.Println("The substring present here is:", substring) fmt.Println("Whether the substring is present in string or not?") found := false for i := 0; i < len(mystr); i++ { if strings.Index(mystr[i:], substring) == 0 { found = true break } } if found { fmt.Println("The string has substring in it.") } else { fmt.Println("The string does not have substring in it.") } } Output The string created here is: Hello, alexa! The substring present here is: alexa Whether the substring is present in string or not? The string has substring in it. Conclusion

We executed the program of checking if a string contains a substring or not using three examples. In the first example we used strings.Contains() function, in the second example we used strings.Index() function and in the third example we used for loop with the former built-in function.

Update the detailed information about Print All Subsequences Of A String In C++ on the Bellydancehcm.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!