Given a string S, the task is to count all possible strings that can be generated by placing spaces between any pair of adjacent characters of the string.
Examples:
Input: S = “AB”
Output: 2
Explanation: All possible strings are { “A B”, “AB”}.
Input: S = “ABC”
Output: 4
Explanation: All possible strings are {“A BC”, “AB C”, “A B C”, “ABC”}
Approach: The problem can be solved by assuming the spaces between adjacent pair of characters of the string as binary bits. Generally, if the length of the string is L, then there L – 1 places to fill by spaces.
Illustration:
S = “ABCD”
Possible places for spaces are:
- Between “A” and “B”
- Between “B” and “C”
- Between “C” and “D”
Length of the string = 4
Possible spaces for spaces = 3 = 4 – 1
Assuming each place to be a binary bit, the total number of possible combinations are:
- 000 -> “ABCD”
- 001 -> “ABC D”
- 010 -> “AB CD”
- 011 -> “AB C D”
- 100 -> “A BCD”
- 101 -> “A BC D”
- 110 -> “A B CD”
- 111 -> “A B C D”
Hence, 8 possible strings can be obtained for a string of length 4.
Therefore, total count of strings = 2 L – 1
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
long long int countNumberOfStrings(string s)
{
int length = s.length();
int n = length - 1;
long long int count = pow (2, n);
return count;
}
int main()
{
string S = "ABCD" ;
cout << countNumberOfStrings(S);
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
#include <string.h>
long long int countNumberOfStrings( char * s)
{
int length = strlen (s);
int n = length - 1;
long long int count = pow (2, n);
return count;
}
int main()
{
char S[] = "ABCD" ;
printf ( "%lld" , countNumberOfStrings(S));
return 0;
}
|
Java
import java.io.*;
class GFG{
static long countNumberOfStrings(String s)
{
int n = s.length() - 1 ;
long count = ( long )(Math.pow( 2 , n));
return count;
}
public static void main(String[] args)
{
String S = "ABCD" ;
System.out.println(countNumberOfStrings(S));
}
}
|
Python3
def countNumberOfStrings(s):
length = len (s)
n = length - 1
count = 2 * * n
return count
if __name__ = = "__main__" :
S = "ABCD"
print (countNumberOfStrings(S))
|
C#
using System;
class GFG{
static long countNumberOfStrings(String s)
{
int n = s.Length - 1;
long count = ( long )(Math.Pow(2, n));
return count;
}
public static void Main(String[] args)
{
string S = "ABCD" ;
Console.WriteLine(countNumberOfStrings(S));
}
}
|
Javascript
<script>
function countNumberOfStrings(s)
{
let n = s.length - 1;
let count = (Math.pow(2, n));
return count;
}
let S = "ABCD" ;
document.write(countNumberOfStrings(S));
</script>
|
Time Complexity: O(log (len – 1)), where len represents length of the given string.
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Apr, 2021
Like Article
Save Article