Prefix of a given String that are divisible by K
Last Updated :
03 Apr, 2023
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:
C++
#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));
}
}
int main()
{
string STR = "998244353" ;
int K = 3;
vector<string> Ans;
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
import java.util.*;
public class GFG
{
public static void prefixes_strings(String STR, int K,
ArrayList<String> Ans)
{
long num = 0 ;
for ( int i = 0 ; i < STR.length(); i++) {
num = num * 10 + (STR.charAt(i) - '0' );
if (num % K == 0 )
Ans.add(Long.toString(num));
}
}
public static void main(String[] args) {
String STR = "998244353" ;
int K = 3 ;
ArrayList<String> Ans = new ArrayList<>();
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) + "' " );
}
}
}
|
Python3
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 = []
prefixes_strings( STR , K, Ans)
print ( "prefixes of a string that are divisible by" , K, ":" , end = " " )
for i in range ( len (Ans)):
print ( , end = " " )
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static void Main()
{
string str = "998244353" ;
int k = 3;
List< string > ans = new List< string >();
PrefixesStrings(str, k, ans);
Console.Write($ "Prefixes of a string that are divisible by {k}: " );
foreach ( string s in ans)
{
Console.Write($ "'{s}' " );
}
}
public static void PrefixesStrings( string str, int k, List< string > ans)
{
long num = 0;
for ( int i = 0; i < str.Length; i++)
{
num = num * 10 + (str[i] - '0' );
if (( ulong )num % ( ulong )k == 0)
{
ans.Add(num.ToString());
}
}
}
}
|
Javascript
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());
}
}
}
let STR = "998244353" ;
let K = 3;
let Ans = [];
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]}' `);
}
|
Output
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)
Share your thoughts in the comments
Please Login to comment...