Given a numeric string S consisting of N digits and a positive integer K, the task is to check if the given string can be split into more than one substrings with difference between consecutive substrings equal to K.
Examples:
Input: S = “8642”, K = 2
Output: Yes
Explanation: Split the given string as {“8”, “6”, “4”, “2”}. Now, the difference between the consecutive substrings is K(= 2).
Input: S = “1009896”, K = 0
Output: No
Approach: The given problem can be solved by generating all possible substring of the given string and check if the concatenation of any subset of the generated substring is equal to the given string S and the consecutive difference of the number as a substring is K, then print Yes. Otherwise, print No. Follow the below steps to solve the problem:
- Iterate over the range [1, N/2] using the variable i and perform the following steps:
- After completing the above steps, if the value of ans is false, print “No”. Otherwise, print “Yes”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void isPossible(string s, int K)
{
bool valid = false ;
long firstx = -1;
int n = s.length();
for ( int i = 1; i <= n / 2; ++i) {
long x = stol(s.substr(0, i));
firstx = x;
string test = to_string(x);
while (test.length() < s.length()) {
x -= K;
test += to_string(x);
}
if (test == s) {
valid = true ;
break ;
}
}
cout << ((valid == true ) ? "Yes " : "No" );
}
int main()
{
string S = "8642" ;
int K = 2;
isPossible(S, K);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void IsPossible(String s, int k)
{
boolean valid = false ;
long firstx = - 1 ;
int n = s.length();
for ( int i = 1 ; i <= n / 2 ; ++i) {
long x = Long.valueOf(s.substring( 0 , i));
firstx = x;
String test = String.valueOf(x);
while (test.length() < s.length()) {
x -= k;
test += String.valueOf(x);
}
if (test.equals(s)) {
valid = true ;
break ;
}
}
System.out.println(valid ? "Yes" : "No" );
}
public static void main(String[] args)
{
String S = "8642" ;
int K = 2 ;
IsPossible(S, K);
}
}
|
Python3
def isPossible(s,K):
valid = False
firstx = - 1
n = len (s)
for i in range ( 1 ,n / / 2 + 1 , 1 ):
x = int (s[ 0 :i])
firstx = x
test = str (x)
while ( len (test) < len (s)):
x - = K
test + = str (x)
if (test = = s):
valid = True
break
print ( "Yes" ) if valid = = True else print ( "No" )
if __name__ = = '__main__' :
S = "8642"
K = 2
isPossible(S, K)
|
C#
using System;
class GFG {
static void IsPossible( string s, int k)
{
bool valid = false ;
long firstx = -1;
int n = s.Length;
for ( int i = 1; i <= n / 2; ++i) {
long x = Convert.ToInt64(s.Substring(0, i));
firstx = x;
string test = x.ToString();
while (test.Length < s.Length) {
x -= k;
test += x.ToString();
}
if (test == s) {
valid = true ;
break ;
}
}
Console.WriteLine(valid == true ? "Yes" : "No" );
}
static void Main( string [] args)
{
string S = "8642" ;
int K = 2;
IsPossible(S, K);
}
}
|
Javascript
<script>
function isPossible(s, K)
{
let valid = false ;
let firstx = -1;
let n = s.length;
for (let i = 1; i <= n / 2; ++i) {
let x = (s.substr(0, i));
firstx = x;
let test = x.toString();
while (test.length < s.length) {
x -= K;
test += x.toString();
}
if (test == s) {
valid = true ;
break ;
}
}
document.write((valid == true ) ? "Yes " : "No" );
}
let S = "8642" ;
let K = 2;
isPossible(S, K);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
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!