Costo mínimo para construir una string.

Dada una string s (que solo contiene letras minúsculas), tenemos que encontrar el costo mínimo para construir la string dada. El costo se puede determinar usando las siguientes operaciones: 

  1. Agregar un solo personaje cuesta 1 unidad 
  2. Se puede agregar una substring de una nueva string (string intermedia) sin costo alguno

Nota* La string intermedia es la string formada hasta ahora. 

C++

// C++ Program to find minimum cost to
// construct a string
#include <iostream>
using namespace std;
 
int minCost(string& s)
{
    // Initially all characters are un-seen
    bool alphabets[26] = { false };
 
    // Marking seen characters
    for (int i = 0; i < s.size(); i++)
        alphabets[s[i] - 97] = true;
 
    // Count total seen character, and that
    // is the cost
    int count = 0;
    for (int i = 0; i < 26; i++)
        if (alphabets[i])
            count++;
 
    return count;
}
 
int main()
{
    // s is the string that needs to be constructed
    string s = "geeksforgeeks";
 
    cout << "Total cost to construct "
         << s << " is " << minCost;
 
    return 0;
}

Java

// Java Program to find minimum cost to
// construct a string
 
class GFG
{
 
    static int minCost(char[] s)
    {
         
        // Initially all characters are un-seen
        boolean alphabets[] = new boolean[26];
 
        // Marking seen characters
        for (int i = 0; i < s.length; i++)
        {
            alphabets[(int) s[i] - 97] = true;
        }
 
        // Count total seen character,
        // and that is the cost
        int count = 0;
        for (int i = 0; i < 26; i++)
        {
            if (alphabets[i])
            {
                count++;
            }
        }
 
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // s is the string that needs to be constructed
        String s = "geeksforgeeks";
        System.out.println("Total cost to construct " +
                s + " is " + minCost(s.toCharArray()));
    }
}
 
// This code is contributed by 29AjayKumar

Python3

# Python 3 Program to find minimum cost to
# construct a string
 
def minCost(s):
     
    # Initially all characters are un-seen
    alphabets = [False for i in range(26)]
 
    # Marking seen characters
    for i in range(len(s)):
        alphabets[ord(s[i]) - 97] = True
 
    # Count total seen character, and that
    # is the cost
    count = 0
    for i in range(26):
        if (alphabets[i]):
            count += 1
 
    return count
 
# Driver Code
if __name__ == '__main__':
     
    # s is the string that needs to
    # be constructed
    s = "geeksforgeeks"
 
    print("Total cost to construct", s,
                      "is", minCost(s))
     
# This code is contributed by
# Surendra_Gangwar

C#

// C# Program to find minimum cost to
// construct a string
using System;
 
class GFG
{
 
    static int minCost(char[] s)
    {
         
        // Initially all characters are un-seen
        bool []alphabets = new bool[26];
 
        // Marking seen characters
        for (int i = 0; i < s.Length; i++)
        {
            alphabets[(int) s[i] - 97] = true;
        }
 
        // Count total seen character,
        // and that is the cost
        int count = 0;
        for (int i = 0; i < 26; i++)
        {
            if (alphabets[i])
            {
                count++;
            }
        }
 
        return count;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        // s is the string that
        // needs to be constructed
        String s = "geeksforgeeks";
        Console.WriteLine("Total cost to construct " +
                s + " is " + minCost(s.ToCharArray()));
    }
}
 
// This code is contributed by Rajput-Ji

Javascript

<script>
      // JavaScript Program to find minimum cost to
      // construct a string
      function minCost(s)
      {
       
        // Initially all characters are un-seen
        var alphabets = new Array(26).fill(0);
 
        // Marking seen characters
        for (var i = 0; i < s.length; i++) {
          alphabets[s[i].charCodeAt(0) - 97] = true;
        }
 
        // Count total seen character,
        // and that is the cost
        var count = 0;
        for (var i = 0; i < 26; i++) {
          if (alphabets[i]) {
            count++;
          }
        }
 
        return count;
      }
 
      // Driver code
      // s is the string that
      // needs to be constructed
      var s = "geeksforgeeks";
      document.write(
        "Total cost to construct " + s + " is " + minCost(s.split(""))
      );
       
      // This code is contributed by rdtank.
    </script>

Publicación traducida automáticamente

Artículo escrito por shubham_rana_77 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *