MCPcopy Create free account
hub / github.com/StudyRust/leetcode_rust / buddy_strings

Function buddy_strings

859-buddy-strings.rs:18–44  ·  view source on GitHub ↗
(s: String, goal: String)

Source from the content-addressed store, hash-verified

16
17
18pub fn buddy_strings(s: String, goal: String) -> bool {
19 if &s.len() != &goal.len() {
20 return false;
21 }
22 if s == goal {
23 let mut s_chars: Vec<char> = s.clone().chars().collect();
24 s_chars.sort_unstable();
25 s_chars.dedup();
26 s_chars.len() < s.len()
27 } else {
28 let mut tmp: Vec<char> = vec!();
29 for (i, c) in s.chars().enumerate() {
30 let o = goal.chars().nth(i).unwrap();
31 if c != o {
32 tmp.push(c);
33 tmp.push(o);
34 }
35 if tmp.len() > 4 {
36 return false;
37 }
38 }
39 if tmp.len() != 4 {
40 return false;
41 }
42 tmp[0] == tmp[3] && tmp[1] == tmp[2]
43 }
44}
45
46fn main() {
47 let s = "abab".to_string();

Callers

nothing calls this directly

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected