Given a 0-indexed based string str of length n consisting of digits, and a positive integer K, the task is to find the prefixes of a String that are exactly Divisible by K. Store the prefix that is divisible by K, in a vector of String and print them. String word str consists of only digits from 0 to 9.
Examples:
Input: Str = “998244353”, K = 3
Output: Prefixes of a String that are Divisible by 3 : “9”, “99”, “998244”, “9982443”.
Explanation: There are only 4 prefixes that are divisible by 3: “9”, “99”, “998244”, and “9982443”.Input: Str = “1010”, K = 10
Output: Prefixes of a String that are Divisible by 10 : “10”, and “1010”.
Explanation: There are only 2 prefixes that are divisible by 10: “10”, and “1010”.
Approach: To solve the problem follow the below steps:
- Create an Ans String Vector to store prefixes of string.
- Create a NUM variable of type long long int and iterate the string and calculate the prefix value, num = num * 10 + (Str[i] – ‘0’).
- Check if that NUM is divisible by K, push back the current prefix to a vector by converting it to the string,
- if (num % (unsigned long long int)K == 0)
- Ans.push_back(to_string(num)).
- Print the Ans string.
Below is the implementation of the above approach:
// Program to print prefixes of string // that are divisible by K #include <bits/stdc++.h> using namespace std;
void prefixes_strings(string STR, int K,
vector<string>& Ans)
{ long long int num = 0;
for ( int i = 0; i < STR.length(); i++) {
num = num * 10 + (STR[i] - '0' );
if (num % (unsigned long long int )K == 0)
Ans.push_back(to_string(num));
}
} // Drivers code int main()
{ string STR = "998244353" ;
int K = 3;
vector<string> Ans;
// Function Call
prefixes_strings(STR, K, Ans);
cout << "prefixes of a string that are divisible by "
<< K << ":" ;
for ( int i = 0; i < Ans.size(); i++) {
cout << "'" << Ans[i] << "'"
<< " " ;
}
return 0;
} |
// Java Program to print prefixes of string // that are divisible by K import java.util.*;
public class GFG
{ // A function to find all prefixes of a
// string that are divisible by K
public static void prefixes_strings(String STR, int K,
ArrayList<String> Ans)
{
// A variable to store the current number
long num = 0 ;
// Iterate through each character in the string
for ( int i = 0 ; i < STR.length(); i++) {
// Convert the current substring to a number
num = num * 10 + (STR.charAt(i) - '0' );
// If the number is divisible by K, add it to the answer vector
if (num % K == 0 )
Ans.add(Long.toString(num));
}
}
// Drivers code
public static void main(String[] args) {
// Input string and the value of K
String STR = "998244353" ;
int K = 3 ;
ArrayList<String> Ans = new ArrayList<>();
// Function Call
prefixes_strings(STR, K, Ans);
System.out.print( "prefixes of a string that are divisible by " + K + ": " );
for ( int i = 0 ; i < Ans.size(); i++) {
System.out.print( "'" + Ans.get(i) + "' " );
}
}
} |
def prefixes_strings( STR , K, Ans):
num = 0
for i in range ( len ( STR )):
num = num * 10 + int ( STR [i])
if num % K = = 0 :
Ans.append( str (num))
STR = "998244353"
K = 3
Ans = []
# Function Call prefixes_strings( STR , K, Ans)
print ( "prefixes of a string that are divisible by" , K, ":" , end = " " )
for i in range ( len (Ans)):
print ( "'" + Ans[i] + "'" , end = " " )
|
// C# Program to print prefixes of string // that are divisible by K using System;
using System.Collections.Generic;
public class GFG {
// Drivers code
public static void Main()
{
// Input string and the value of K
string str = "998244353" ;
int k = 3;
List< string > ans = new List< string >();
// Function call
PrefixesStrings(str, k, ans);
Console.Write($ "Prefixes of a string that are divisible by {k}: " );
foreach ( string s in ans)
{
Console.Write($ "'{s}' " );
}
}
// A function to find all prefixes of a
// string that are divisible by K
public static void PrefixesStrings( string str, int k, List< string > ans)
{
// A variable to store the current number
long num = 0;
// Iterate through each character in the string
for ( int i = 0; i < str.Length; i++)
{
// Convert the current substring to a number
num = num * 10 + (str[i] - '0' );
// If the number is divisible by K, add it to the answer vector
if (( ulong )num % ( ulong )k == 0)
{
ans.Add(num.ToString());
}
}
}
} |
function prefixes_strings(STR, K, Ans) {
let num = 0;
for (let i = 0; i < STR.length; i++) {
num = num * 10 + (STR.charCodeAt(i) - "0" .charCodeAt(0));
if (num % K === 0) {
Ans.push(num.toString());
}
}
} // Drivers code let STR = "998244353" ;
let K = 3; let Ans = []; // Function Call prefixes_strings(STR, K, Ans); console.log(`prefixes of a string that are divisible by ${K}:`); for (let i = 0; i < Ans.length; i++) {
console.log(` '${Ans[i]}' `);
} |
prefixes of a string that are divisible by K:'9' '99' '998244' '9982443'
Time Complexity: O(N) where N is the length of the string
Auxiliary Space: O(1)