📜  K远字符串

📅  最后修改于: 2021-05-04 22:24:33             🧑  作者: Mango

给定一个长度为n的字符串和一个非负整数k。查找给出K字符串的遥远的字符串。

两个字母之间的距离是它们在字母表中的位置之间的差。例如:

  • dist(c,e)= dist(e,c)= 2。
  • dist(a,z)= dist(z,a)= 25。

通过使用此概念,两个字符串之间的距离是相应字母的距离之和。例如 :

  • dist(af,hf)= dist(a,h)+ dist(f,f)= 7 + 0 = 7。

给定一个字符串和一个距离k。任务是找到一个字符串,使结果字符串与给定字符串的距离为k。如果无法使用k个远距字符串,则打印“否”。
注意:可能存在多种解决方案。我们急需找到其中一个。

例子 :

Input : bear
        k = 26
Output : zcar
Here, dist(bear, zcar) = 
      dist(b, z) + dist(e, c) +
      + dist(a, a) + dist(r, r)
      = 24 + 2 + 0 + 0 
      = 26

Input : af
        k = 7
Output : hf
Here, dist(af, hf) = dist(a, h) + dist(f, f) 
                   = 7 + 0             
                   = 7

Input : hey
        k = 1000
Output : No
Explanation :
No such string exists.

如果给定的所需距离太大,则无法解决。考虑给定字符串的最大可能距离是多少。还是更有用的东西-如何构造丢失的字符串以最大化距离?分别对待每个字母,并用距离最远的字母替换。例如,我们应将“ c”替换为“ z”,并将“ y”替换为“ a”。更准确地说,对于字母表的前13个字母,最远的字母是“ z”,对于其他字母,它是“ a”。

该方法很简单,遍历给定字符串的字母并贪婪地更改它们。 “贪婪”一词意味着在更改字母时,不必关心接下来的字母。通常,必须有距离较远的字母,因为否则可能没有解决方案。对于给定字符串的每个字母,请将其更改为最远的字母,除非总距离太大。更改字母后,请减少剩余的所需距离。因此,对于给定字符串的每个字母,仅考虑不超过剩余距离的字母,并从中选择距离最远的字母。

CPP和Java实施:

CPP
// CPP program to find the k distant string
#include 
using namespace std;
  
// function to find the
// lost string
string findKDistantString(string str, int k)
{
    int n = str.length();
  
    for (int i = 0; i < n; ++i) {
  
        char best_letter = str[i];
        int best_distance = 0;
  
        for (char maybe = 'a'; 
             maybe <= 'z'; ++maybe) 
        {
            int distance = abs(maybe - str[i]);
  
            // check if "distance <= k",
            // so that, the total distance
            // will not exceed among
            // letters with "distance <= k"
            if (distance <= k && distance >
                              best_distance) 
            {
                best_distance = distance;
                best_letter = maybe;
            }
        }
  
        // decrease the remaining
        // distance
        k -= best_distance;
        str[i] = best_letter;
          
    }
      
    assert(k >= 0);
    // we found a correct
    // string only if "k == 0"
    if (k > 0)
        return "No";
    else
        return str;
}
  
// driver function
int main()
{
    string str = "bear";
    int k = 26;
    cout << findKDistantString(str, k) << endl;
  
    str = "af";
    k = 7;
      
    cout << findKDistantString(str, k) << endl;
    return 0;
}


Java
// Java program to find k distant string
import java.util.*;
import java.lang.*;
  
public class GfG {
      
    // function to find
    // the lost string
    public static String findKDistantString
                       (String str1, int k)
    {
        int n = str1.length();
        char[] str = str1.toCharArray();
          
        for (int i = 0; i < n; ++i) {
            char best_letter = str[i];
            int best_distance = 0;
              
            for (char maybe = 'a'; 
                 maybe <= 'z'; ++maybe) 
            {
                int distance = 
                    Math.abs(maybe - str[i]);
  
                // Check if "distance <= k"
                // so that it should not
                // exceed the total distance
                // among letters with "distance
                // <= k" we choose the most
                // distant one
                if (distance <= k && distance 
                                > best_distance) 
                {
                    best_distance = distance;
                    best_letter = maybe;
                }
            }
  
            // we decrease the remaining
            // distance
            k -= best_distance;
            str[i] = best_letter;
        }
          
        assert(k >= 0);
          
        // Correct string only
        // if "k == 0"
        if (k > 0)
            return "No";
        else
            return (new String(str));
    }
    public static void main(String argc[])
    {
        String str = "bear";
        int k = 26;
        System.out.println(findKDistantString(str, k));
  
        str = "af";
        k = 7;
        System.out.println(findKDistantString(str, k));
    }
}


Python3
# Python implementation to check if
# both halves of the string have
# at least one different character
  
MAX = 26
  
# Function which break string into two halves
# Counts frequency of characters in each half
# Compares the two counter array and returns
# true if these counter arrays differ
def function(st):
    global MAX
    l = len(st)
      
    # Declaration and initialization
    # of counter array
    counter1, counter2 = [0]*MAX, [0]*MAX
      
    for i in range(l//2):
        counter1[ord(st[i]) - ord('a')] += 1
  
    for i in range(l//2, l):
        counter2[ord(st[i]) - ord('a')] += 1
  
    for i in range(MAX):
        if (counter2[i] != counter1[i]):
            return True
  
    return False
  
  
# Driver function
st = "abcasdsabcae"
if function(st): print("Yes, both halves differ by at least one character")
else: print("No, both halves do not differ at all")
  
# This code is contributed by Ansu Kumari


C#
// C# program to find k distant string
using System;
  
class GfG {
      
    // function to find the lost string
    public static String findKDistantString
                       (string str1, int k)
    {
        int n = str1.Length;
        char []str = str1.ToCharArray();
          
        for (int i = 0; i < n; ++i) {
            char best_letter = str[i];
            int best_distance = 0;
              
            for (char maybe = 'a'; 
                maybe <= 'z'; ++maybe) 
            {
                int distance = 
                    Math.Abs(maybe - str[i]);
  
                // Check if "distance <= k"
                // so that it should not
                // exceed the total distance
                // among letters with "distance
                // <= k" we choose the most
                // distant one
                if (distance <= k && distance 
                                > best_distance) 
                {
                    best_distance = distance;
                    best_letter = maybe;
                }
            }
  
            // we decrease the remaining
            // distance
            k -= best_distance;
            str[i] = best_letter;
        }
          
        //(k >= 0);
          
        // Correct string only
        // if "k == 0"
        if (k > 0)
            return "No";
        else
            return (new string(str));
    }
      
    // Driver code
    public static void Main()
    {
        string str = "bear";
        int k = 26;
        Console.WriteLine(
              findKDistantString(str, k));
  
        str = "af";
        k = 7;
        Console.Write(
              findKDistantString(str, k));
    }
}
  
// This code is contributed by Nitin millal.


输出:

zcar
hf