1. 程式人生 > >LeetCode 833 Find And Replace in String

LeetCode 833 Find And Replace in String

LeetCode 833

Find And Replace in String

  • Problem Description:
    題目給出四個已知條件:進行操作的字串S,indexes陣列(儲存S中需進行替換的位置下標),sources陣列(需要匹配S中有indexes陣列中下標的子字串是否與sources中對應子字串相同),targets陣列(如果匹配成功,將S中有indexes陣列中下標的子字串替換為targets中對應的子字串)

  • Example: 題目給出的例子如下所示
  • Solution:
    • 解題思路:
      (1)遍歷S字串的每個位置,在indexes陣列中查詢該位置是否需要進行替換,如果不需要,則將temp += S中對應字元

      (2)如果S字串的某個位置在indexes陣列中可以查詢到,則比較從該起始位置起一段長度的字串與sources陣列中對應的字串是否相同,如果不相同不進行替換,temp += S中對應字串。如果相同,用targets陣列中對應的字串替換S中對應字串,注意將遍歷S字串的下標進行相應的移動。
    • 程式設計實現:
class Solution {
public:
    string findReplaceString(string S, vector<int>& indexes, vector<string>& sources, vector<string
>
& targets) { if (indexes.size() == 0) return S; string temp = ""; int flag = 0, j; for (int i = 0; i < S.length(); ) { for (j = 0;j < indexes.size(); j++) { if (i == indexes[j]) { if (S.substr(i, sources[j].length()) == sources[j]) { temp += targets[j]; } else
{ temp += S.substr(i, sources[j].length()); } flag = 1; break; } } if (flag == 0) { temp += S[i]; i++; } else { flag = 0; i+=sources[j].length(); } } return temp; } };

相關推薦

LeetCode 833 Find And Replace in String

LeetCode 833 Find And Replace in String Problem Description: 題目給出四個已知條件:進行操作的字串S,indexes陣列(儲存S中

833. Find And Replace in String

neo bsp overlap nbsp source and there bec enc Find And Replace in String To some string S, we will perform some replacement operations th

[LeetCode] Find And Replace in String 在字符串中查找和替換

ant The osi then rep nes art onos abc To some string S, we will perform some replacement operations that replace groups of letters wit

LeetCode - 890. Find and Replace Pattern

You have a list of words and a pattern, and you want to know which words in words matches the pattern. A word matches the p

LeetCode: 890. Find and Replace Pattern

題目:890. Find and Replace Pattern(https://leetcode.com/problems/find-and-replace-pattern/description/) 解法: 將元素的出現的位置放入陣列中,當元素第一次出現時,他在陣列中的值是預設

JavaScript刷LeetCode -- 890. Find and Replace Pattern

一、題目 You have a list of words and a pattern, and you want to know which words in words matches the pattern. A word matches the pattern if ther

[leetcode-438-Find All Anagrams in a String]

not plan english urn tco bst ice style ons Given a string s and a non-empty string p, find all the start indices of p‘s anagrams in s.Str

leetcode-438-Find All Anagrams in a String

IT -- 新的 iss HA nag cpp mat strings 題目描述: Given a string s and a non-empty string p, find all the start indices of p‘s anagrams in s. Str

LeetCode 205. Isomorphic Strings; 290. Word Pattern; 890. Find and Replace Pattern

string lse cto return 建立 flag 一次 else leetcode 這幾道題都是pattern的題目, Isomorphic Strings 和 Word Pattern 是完全一樣的問題,Find and Replace Pattern 本質也一

leetcodeFind All Anagrams in a String(438)

題目: 給定一個字串 s 和一個非空字串 p,找到 s 中所有是 p 的字母異位詞的子串,返回這些子串的起始索引。 字串只包含小寫英文字母,並且字串 s 和 p 的長度都不超過 20

python leetcode 438. Find All Anagrams in a String

可以用字典做 也可以用再加個函式判斷是否是Anagrams class Solution: def findAnagrams(self, s, p): """ :type s: str :type p: str :r

[LeetCode] 438. Find All Anagrams in a String

題目描述 Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English

leetcode 438. Find All Anagrams in a String

temp class int ram while lee all () 正在 題意:在s串中找到與p串所用字符種類,數目相同的所有子串的起始位置。初始化兩個變量start,end,diff。start,end為s串中目前正在與p串比較的子串的起始位置和終止位置。diff為這

LeetCode Find and Replace Pattern 查詢和替換模式

You have a list of words and a pattern, and you want to know which words in words matches the pattern. A word matches the pattern if ther

leetcode【890】 Find and Replace Pattern(來來來,你教我怎麼不用靜態變數做這道題?)

寫在最前面: 我真的是無語,本地和leetcode執行程式碼都過了,就是提交過不了,來來來,你教我怎麼不用靜態變數做這道題??? You have a list of words and a pattern, and you want to know which

[python]leetcode(438). Find All Anagrams in a String

problem Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s. Strings consists of

[Leetcode] 438. Find All Anagrams in a String 解題報告

題目: Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Strings consists of lowercase English

Leetcode 438. Find All Anagrams in a String (Easy) (cpp)

Leetcode 438. Find All Anagrams in a String (Easy) (cpp) Tag: Hash Table Difficulty: Easy /* 43

leetcode No438. Find All Anagrams in a String

Question: Given a string s and a non-empty string p, find all the start indices of p's anagrams in

LeetCode 438. Find All Anagrams in a String(找到字串中所有字母異位詞)

Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s. Strings consists of lowercase English