📜  查找具有给定功率的子串

📅  最后修改于: 2022-05-13 01:57:07.461000             🧑  作者: Mango

查找具有给定功率的子串

给定字符串小写字母,任务是找到具有给定幂的子串的索引。假设 a 的幂为 1,b 的幂为 2,c 的幂为 3,依此类推。这里子串的幂是指该特定子串中所有字符的幂的总和。
例子:

简单的方法:
1. 使用嵌套的 for 循环计算所有子串的幂。
2. 如果任何子串的幂等于给定的幂,则打印子串的索引。
3. 如果不存在这样的子串,则打印“No substring with given power exists”。
时间复杂度: O(n^2)。
有效方法:使用地图存储权力。
1. 对于每个元素,检查 curr_power - power 是否存在于地图中。
2. 如果它存在于映射中,则意味着我们有一个具有给定幂的子字符串,否则我们将 curr_power 插入映射并继续下一个字符。
3. 如果字符串的所有字符都被处理并且我们没有找到任何具有给定幂的子字符串,则子字符串不存在。

C++
// C++ program to find substring with given power
#include 
#define ll long long int
using namespace std;
 
// Function to print indexes of substring with
// power as given power.
void findSubstring(string str, ll power)
{
    ll i;
    // Create an empty map
    unordered_map map;
 
    // Maintains sum of powers of characters so far.
    int curr_power = 0;
    int len = str.length();
 
    for (i = 0; i < len; i++) {
        // Add current character power to curr_power.
        curr_power = curr_power + (str[i] - 'a' + 1);
 
        // If curr_power is equal to target power
        // we found a substring starting from index 0
        // and ending at index i.
        if (curr_power == power) {
            cout << "Substring from index " << 0 << " to "
                 << i << " has power " << power << endl;
            return;
        }
 
        // If curr_power - power already exists in map
        // then we have found a subarray with target power.
        if (map.find(curr_power - power) != map.end()) {
            cout << "Substring from index "
                 << map[curr_power - power] + 1
                 << " to " << i <<" has power " <


Java
// Java program to find substring with given power
import java.util.*;
 
class GFG
{
 
    // Function to print indexes of substring with
    // power as given power.
    static void findSubstring(String str, int power)
    {
 
        // Create an empty map
        HashMap map = new HashMap<>();
 
        // Maintains sum of powers of characters so far.
        int curr_power = 0;
        int len = str.length();
 
        for (int i = 0; i < len; i++)
        {
 
            // Add current character power to curr_power.
            curr_power = curr_power + (str.charAt(i) - 'a' + 1);
 
            // If curr_power is equal to target power
            // we found a substring starting from index 0
            // and ending at index i.
            if (curr_power == power)
            {
                System.out.println("Substring from index 0" +
                                   " to " + i + " has power " + power);
                return;
            }
 
            // If curr_power - power already exists in map
            // then we have found a subarray with target power.
            if (map.containsKey(curr_power - power))
            {
                System.out.println("Substring from index " +
                                  (map.get(curr_power - power) + 1) +
                                 " to " + i + " has power " + power);
                return;
            }
 
            map.put(curr_power, i);
        }
 
        // If we reach here, then no substring exists.
        System.out.println("No substring with given power exists.");
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String str = "geeksforgeeks";
        int power = 36;
 
        findSubstring(str, power);
    }
}
 
// This code is contributed by
// sanjeev2552


C#
// C# program to find substring
// with given power
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Function to print indexes of substring
    // with power as given power.
    static void findSubstring(String str,
                              int power)
    {
 
        // Create an empty map
        Dictionary map = new Dictionary();
 
        // Maintains sum of powers
        // of characters so far.
        int curr_power = 0;
        int len = str.Length;
 
        for (int i = 0; i < len; i++)
        {
 
            // Add current character power
            // to curr_power.
            curr_power = curr_power +
                        (str[i] - 'a' + 1);
 
            // If curr_power is equal to target power
            // we found a substring starting from index 0
            // and ending at index i.
            if (curr_power == power)
            {
                Console.WriteLine("Substring from index 0" +
                                  " to " + i +
                                  " has power " + power);
                return;
            }
 
            // If curr_power - power already exists in map
            // then we have found a subarray with target power.
            if (map.ContainsKey(curr_power - power))
            {
                Console.WriteLine("Substring from index " +
                            (map[curr_power - power] + 1) +
                       " to " + i + " has power " + power);
                return;
            }
             
            if (!map.ContainsKey(curr_power))
                    map.Add(curr_power, i);
            else
                map[curr_power] = i;
        }
 
        // If we reach here, then no substring exists.
        Console.WriteLine("No substring with " +
                          "given power exists");
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String str = "geeksforgeeks";
        int power = 36;
 
        findSubstring(str, power);
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
Substring from index 3 to 5 has power 36

时间复杂度: O (n)