Go語言程式檢查字串是否為兩個不同字串的有效混排


在 Go 程式語言中,字串是一種內建的資料型別,表示字元序列。它們使用雙引號 (") 定義,可以包含任何有效的 Unicode 字元。在本文中,我們將學習如何使用不同的示例集來檢查字串是否為兩個字串的有效混排。輸出將是使用 fmt 包列印到控制檯上的布林值。

方法:使用帶有 len 方法的 for 迴圈

在本方法中,我們將學習如何檢查字串是否為兩個不同字串的有效混排。此程式定義了一個名為 isShuffle(str1, str2, shuffle string) bool 的函式,該函式接受三個字串作為引數:str1 和 str2 是兩個單獨的原始字串,shuffle 是要檢查的字串,以檢視它是否已成功與 str1 和 str2 混排。

演算法

  • 步驟 1 − 建立一個包 main 並宣告程式中的 fmt(格式包)包,其中 main 生成可執行示例,而 fmt 幫助格式化輸入和輸出。

  • 步驟 2 − 建立一個函式 isShuffle,並在該函式中將 mystr1、mystr2 和混排字串作為輸入。

  • 步驟 3 − i、j 和 k 都應初始化為 0。

  • 步驟 4 − 從 0 開始重複混排字串的長度 k 次。

  • 步驟 5 − 每次檢查 i 是否小於 mystr1 的長度,以及 mystr1 的第 i 個字元是否與混排字串的第 k 個字元對應。如果是,則增加 i。

  • 步驟 6 − 如果上述條件為假,則檢查 j 是否小於 mystr2 的長度,以及 mystr2 的第 j 個字元是否與混排字串的第 k 個字元相同。如果是,則增加 j。

  • 步驟 7 − 如果上述語句均不正確,則返回 false,並且在迴圈完成後返回 true。

  • 步驟 8 − 透過將混排字串的字元與原始字串的字元進行比較,上述方法採用單指標方法 (i,j,k) 來確定混排字串是否為兩個單獨字串的有效混排。

示例

在本例中,我們將使用帶有 len 函式的 for 迴圈來檢查 Go 語言中字串是否為兩個不同字串的有效混排。

package main
import "fmt"

func isShuffle(mystr1, mystr2, shuffle string) bool {
   if len(mystr1)+len(mystr2) != len(shuffle) {
      return false
   }
   i, j, k := 0, 0, 0
   for k < len(shuffle) {
      if i < len(mystr1) && mystr1[i] == shuffle[k] {
         i++
      } else if j < len(mystr2) && mystr2[j] == shuffle[k] {
         j++
      } else {
         return false
      }
      k++
   }
   return true
}

func main() {
   mystr1 := "abc"    //create a string1
   fmt.Println("The string1 given here is:", mystr1)
   mystr2 := "def"     //create string2
   fmt.Println("The string2 given here is:", mystr2)
   fmt.Println("Is the string a valid shuffle of two strings?")
   shuffle := "abcdef"
   fmt.Println(isShuffle(mystr1, mystr2, shuffle)) //check if the shuffle is valid
}

輸出

The string1 given here is: abc
The string2 given here is: def
Is the string a valid shuffle of two strings?
true

方法 2:使用 rune 方法

在本方法中,我們將使用 for 迴圈檢查字串是否為兩個不同字串的有效混排。此程式定義的 isShuffle 函式接受三個字串:mystr1、mystr2 和 shuffle。該函式使用兩個指標遍歷混排字串中的每個字元,每個輸入字串一個。對於混排字串中的每個字元,該函式檢查當前字元和 mystr1 或 mystr2 的當前指標處的字元是否匹配。

演算法

  • 步驟 1 − 建立一個包 main 並宣告程式中的 fmt(格式包)包,其中 main 生成可執行示例,而 fmt 幫助格式化輸入和輸出。

  • 步驟 2 − 初始化兩個指標 i 和 j,分別指向輸入字串 mystr1 和 mystr2 的開頭。

  • 步驟 3 − 遍歷混排字串中的每個字元後。對於混排字串中的每個字元,檢查當前字元和 mystr1 或 mystr2 的當前指標處的字元是否匹配。

  • 步驟 4 − 如果當前字元與 mystr1 的當前指標處的字元匹配,則增加 mystr1 的指標。

  • 步驟 5 − 如果當前字元與 mystr2 的當前指標處的字元匹配,則增加 mystr2 的指標。

  • 步驟 6 − 如果任一輸入字串的當前字元與當前指標處的字元不匹配(表明混排字串不是兩個輸入字串的有效混排),則返回 false。

  • 步驟 7 − 如果兩個輸入字串的指標都已到達其各自字串的末尾(表明混排字串是兩個輸入字串的有效混排),則返回 true。

  • 步驟 8 − 輸出將是使用 fmt.Println() 函式列印到控制檯上的布林值,其中 ln 表示換行符。

示例

在本例中,我們將使用 rune 方法來檢查 Go 語言中字串是否為兩個不同字串的有效混排。

package main
import "fmt"

func isShuffle(mystr1, mystr2, shuffle_val string) bool {

   i, j := 0, 0
   
   // Iterate over each character in the shuffle string
   for _, c := range shuffle_val {
      if i < len(mystr1) && c == rune(mystr1[i]) {
         i++
      } else if j < len(mystr2) && c == rune(mystr2[j]) {
         j++
      } else {
         return false
      }
   }
   return i == len(mystr1) && j == len(mystr2)
}

func main() {
   // Test cases
   fmt.Println("Whether the string is a valid shuffle of two distinct strings?")
   fmt.Println(isShuffle("abc", "def", "dabecf")) // true
}

輸出

Whether the string is a valid shuffle of two distinct strings?
true

結論

我們使用兩個示例執行了檢查字串是否為兩個字串的有效混排的程式。在第一個示例中,我們使用了帶有 len 方法的 for 迴圈,在第二個示例中,我們使用了 rune 方法。

更新於: 2023年2月20日

瀏覽量 182 次

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告