You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何合并两个字符串并替换重复内容?求推荐实现方案

Merging Strings with Duplicate Phrase/Content Removal: Approach Breakdown

Great question—handling duplicate detection when merging strings gets way more nuanced once you move past simple exact matches. Let’s walk through the two approaches you’re considering, their pros/cons, and a recommended path for complex scenarios.

First Approach: Split into Arrays + Recursive Phrase Traversal

This method targets multi-word duplicate phrases (not just single words), which is exactly what you’ll need for complex use cases. Here’s how it stacks up:

  • Pros:
    • Can identify longer repeated chunks (e.g., "quick brown fox" appearing in both strings) instead of just isolated words, leading to cleaner merged results.
    • Recursion lets you check phrase lengths from longest to shortest, which prevents you from replacing a short word and accidentally breaking up a longer matching phrase.
  • Cons:
    • Recursion can introduce performance bottlenecks with very long strings if not optimized (you might hit stack limits or unnecessary repeated checks).
    • Splitting on spaces alone ignores punctuation (e.g., "fox." vs. "fox" would be treated as different) and messy whitespace (multiple spaces, tabs), so you’ll need preprocessing to normalize the input first.

Second Approach: Individual Word Checks with indexOf

This is the simpler route, but it’s limited for complex scenarios:

  • Pros:
    • Super straightforward to implement, with minimal performance overhead.
    • Works perfectly if your only concern is single-word duplicates in simple contexts.
  • Cons:
    • Cannot detect multi-word repeated phrases—a huge gap for complex use cases. For example, if both strings contain "the quick brown fox", this method would treat each word as a separate duplicate, leading to a jumbled merged string instead of cleaning up the full repeated phrase.
    • Risks over-removing words that appear in different contexts (e.g., "the" in "the cat" and "the dog" shouldn’t be removed, but this method might flag it as a duplicate).

Skip pure recursion (to avoid performance issues) and instead use an optimized sliding-window approach for phrase matching, paired with preprocessing:

  1. Preprocess your strings first:
    • Normalize case (convert to lower/upper case to ensure case-insensitive matching).
    • Strip or standardize punctuation (e.g., replace "fox." with "fox", "fox!" with "fox").
    • Collapse multiple spaces/tabs into a single space, and trim leading/trailing whitespace.
  2. Use a sliding window to find longest matching phrases first:
    Start with the longest possible phrase length (the length of the shorter string’s word array) and work your way down. This ensures you catch the biggest duplicates first, so shorter word matches don’t interfere.
    Here’s a simplified pseudocode example to illustrate:
    // Preprocessed word arrays from both strings
    string[] arr1 = preprocessString(str1).Split(' ');
    string[] arr2 = preprocessString(str2).Split(' ');
    
    int maxPhraseLength = Math.Min(arr1.Length, arr2.Length);
    
    // Check from longest to shortest phrases
    for (int phraseLen = maxPhraseLength; phraseLen >= 1; phraseLen--) {
        // Scan arr1 for all phrases of current length
        for (int i = 0; i <= arr1.Length - phraseLen; i++) {
            string[] currentPhrase = arr1.Skip(i).Take(phraseLen).ToArray();
            // Check if this phrase exists in arr2
            int matchIndex = FindPhraseInArray(arr2, currentPhrase);
            if (matchIndex != -1) {
                // Merge: keep arr1's phrase, remove the duplicate from arr2
                List<string> mergedList = arr1.ToList();
                mergedList.AddRange(arr2.Take(matchIndex));
                mergedList.AddRange(arr2.Skip(matchIndex + phraseLen));
                return string.Join(" ", mergedList);
            }
        }
    }
    
    // No duplicates found—just concatenate
    return string.Join(" ", arr1) + " " + string.Join(" ", arr2);
    
  3. Add context checks if needed:
    If you only want to merge duplicates that appear at the end of the first string and start of the second (common use case for merging sentences), adjust the logic to only check phrases at the tail of arr1 and head of arr2.

Key Notes

  • Be clear on what counts as a "duplicate": Are you targeting consecutive repeated phrases (like overlapping sentence endings/beginnings) or any repeated word/phrase anywhere? Adjust your matching logic accordingly.
  • For very large strings, optimize by first using a hash map to count word occurrences—you can skip checking phrases that include words only present in one string.

内容的提问来源于stack exchange,提问作者Barry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:29:42