MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / anagram_solution3

Function anagram_solution3

code/chapter02/anagram_solution3.rs:1–23  ·  view source on GitHub ↗
(s1: &str, s2: &str)

Source from the content-addressed store, hash-verified

1fn anagram_solution3(s1: &str, s2: &str) -> bool {
2 if s1.len() != s2.len() { return false; }
3
4 // s1 和 s2 中的字符分别加入 alist, blist 并排序
5 let mut alist = Vec::new();
6 let mut blist = Vec::new();
7 for c in s1.chars() { alist.push(c); }
8 for c in s2.chars() { blist.push(c); }
9 alist.sort(); blist.sort();
10
11 // 逐个比较排序的集合,任何字符不匹配就退出循环
12 let mut pos: usize = 0;
13 let mut ok = true;
14 while pos < alist.len() && ok {
15 if alist[pos] == blist[pos] {
16 pos += 1;
17 } else {
18 ok = false;
19 }
20 }
21
22 ok
23}
24
25fn main() {
26 let s1 = "rust";

Callers 1

mainFunction · 0.70

Calls 3

lenMethod · 0.45
pushMethod · 0.45
sortMethod · 0.45

Tested by

no test coverage detected