Convert all substrings of length ‘k’ from base ‘b’ to decimal

A string defining a valid number is given. Output all the base conversions of substrings of length ‘k’ from base ‘b’ to base 10.

Examples:

Input :  str = "12212", 
      k = 3, b = 3.
Output : 17 25 23
Explanation :
All the substrings of length 'k' are : 122, 221, 212.
Base conversion can be computed using the formula.

Method 1 (Simple)

A simple approach is to use simple base conversion technique. For a base b number str, its decimal equivalent is str[0]*b0 + str[1]*b1 + str[2]*b2 + … + str[n-1]*bn-1

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple C++ program to convert all substrings from
// decimal to given base.
#include <bits/stdc++.h>
using namespace std;
  
int substringConversions(string str, int k, int b)
{
    for (int i=0; i + k <= str.size(); i++)
    {
        // Saving substring in sub
        string sub = str.substr(i, k);        
          
        // Evaluating decimal for current substring
        // and printing it.
        int sum = 0, counter = 0;
        for (int i = sub.size() - 1; i >= 0; i--)
        {
            sum = sum + ((sub.at(i) - '0') * pow(b, counter));
            counter++;
        }        
        cout << sum << " ";
    }
}
  
// Driver code
int main()
{
    string str = "12212";
    int b = 3, k = 3;
    substringConversions(str, b, k);    
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple Java program to convert all substrings from
// decimal to given base.
  
class GFG
{
  
static void substringConversions(String str, int k, int b)
{
    for (int i=0; i + k <= str.length(); i++)
    {
        // Saving substring in sub
        String sub = str.substring(i, i+k);     
          
        // Evaluating decimal for current substring
        // and printing it.
        int sum = 0, counter = 0;
        for (int j = sub.length() - 1; j >= 0; j--)
        {
            sum = (int) (sum + ((sub.charAt(j) - '0') *
                                    Math.pow(b, counter)));
            counter++;
        }     
        System.out.print(sum + " ");
    }
}
  
// Driver code
public static void main(String[] args)
{
    String str = "12212";
    int b = 3, k = 3;
    substringConversions(str, b, k); 
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Simple Python3 program to convert 
# all substrings from decimal to given base.
import math
  
def substringConversions(s, k, b):
      
    l = len(s);
    for i in range(l):
          
        if((i + k) < l + 1):
              
            # Saving substring in sub
            sub = s[i : i + k];     
              
            # Evaluating decimal for current 
            # substring and printing it.
            sum, counter = 0, 0;
            for i in range(len(sub) - 1, -1, -1):
      
                sum = sum + ((ord(sub[i]) - ord('0')) *     
                              pow(b, counter));
                counter += 1;
              
            print(sum , end = " ");
  
# Driver code
s = "12212";
b, k = 3, 3;
substringConversions(s, b, k); 
  
# This code is contributed 
# by Princi Singh

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple C# program to convert all substrings from
// decimal to given base.
using System; 
  
class GFG
{
  
static void substringConversions(String str, int k, int b)
{
    for (int i = 0; i + k <= str.Length; i++)
    {
        // Saving substring in sub
        String sub = str.Substring(i, k);     
          
        // Evaluating decimal for current substring
        // and printing it.
        int sum = 0, counter = 0;
        for (int j = sub.Length - 1; j >= 0; j--)
        {
            sum = (int) (sum + ((sub[j] - '0') *
                                    Math.Pow(b, counter)));
            counter++;
        }     
        Console.Write(sum + " ");
    }
}
  
// Driver code
public static void Main(String[] args)
{
    String str = "12212";
    int b = 3, k = 3;
    substringConversions(str, b, k); 
}
}
  
/* This code is contributed by PrinciRaj1992 */

chevron_right