簡體   English   中英

在隨機字符串中查找確切單詞的字母並在 c# 中刪除這些字母

[英]Finding the letters of an exact word in a random string and remove this letters in c#

我看到了這個任務,我試圖解決它,但我被卡住了,我將上傳我到目前為止所做的代碼。 我是關閉還是我的解決方案完全錯誤? 請讓我知道正確的解決方案。

給出一個由大寫英文字母組成的字符串 S。 一步我們可以從S中刪除七個字母,形成單詞“BALLOON”(一個'B',一個'A',兩個'L',兩個'0'和一個'N'),並在S中留下一個較短的單詞. 如果縮短的 S 中的剩余字母足以允許刪除單詞“BALLOON”的另一個實例,則可以進行下一步。 我們可以對 S 應用的此類移動的最大數量是多少?

public int solution(string S)
{
    char[] array = new char[] { 'B', 'A', 'L', 'L', 'O', 'O', 'N' };
    char[] sIntoCharacters = S.ToCharArray();
    int count = 0;
    for (int i = 0; i < sIntoCharacters.Length; i++)
    {
        for (int j = 0; j < array.Length; j++)
        {
            if (sIntoCharacters[i] == array[j])
            {
                S.Trim(new char[] { 'B', 'A', 'L', 'L', 'O', 'O', 'N' });
                count++;
            }
        }
    }
    return count;
}

執行此操作的有效方法(與您的方法完全不同)如下:

  1. 計算輸入中每個字母的出現次數。
  2. 計算目標中每個字母的出現次數(在本例中,目標是“BALLOONS”)。
  3. 對於目標計數中字符的每個非零計數,計算輸入計數除以目標計數。
  4. 計算所有這些除法的最小值。

這將為您提供輸入中目標的最大出現次數。

這具有O(N+M)的復雜度,其中N是輸入中的字符數, M是目標中的字符數。

示例實現:

using System;
using System.Linq;

namespace Demo
{
    static class Program
    {
        public static void Main()
        {
            string input  = Randomise("BALLOONSBALLOONSBALLOONSBALLOONSABCDEFGHIJKLMNOPQRSTUVWXYS");
            string target = "BALLOONS";

            Console.WriteLine($"Input: {input}");
            Console.WriteLine($"Number of occurrences of {target} = {CountOccurrences(input, target)}");
        }

        public static string Randomise(string input)
        {
            Random rng = new Random(); // Used to randomise the input string.
            return new string(input.OrderBy(c => rng.Next()).ToArray());
        }

        public static int CountOccurrences(string input, string target)
        {
            int[] inputCounts  = CountLetters(input);
            int[] targetCounts = CountLetters(target);

            // Now for each non-zero value in targetCounts[], calculate inputCounts[x]/targetCounts[x]
            // and calculate the minimum of all those values. That will be the answer.

            int min = int.MaxValue;

            for (int i = 0; i < 26; ++i)
            {
                if (targetCounts[i] != 0)
                    min = Math.Min(min, inputCounts[i]/targetCounts[i]);
            }

            // Handle the case where targetCounts[] was all zeroes, where min would still be int.MaxValue.
            return min == int.MaxValue ? 0 : min; 
        }

        public static int[] CountLetters(string text)
        {
            int[] result = new int[26]; // For 'A'..'Z' counts.

            foreach (char c in text)
            {
                int index = char.ToUpperInvariant(c) - 'A';

                if (index >= 0 && index < 26)
                    ++result[index];
            }

            return result;
        }
    }
}

示例 output(每次運行的“輸入”字符串會有所不同,因為它是隨機的,但答案始終為 4):

Input: KNOOSSCNLLVGLLAOBNOFAWBHRSLAISSBUSLAJLBLOOXNTOBMDAOPQNOEYL
Number of occurrences of BALLOONS = 4

Randomise()方法僅用於演示使用隨機輸入運行的解決方案,而不是使用重復多次“BALLOON”一詞的東西。 它實際上不是解決方案的一部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM