diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/bus710.go b/construct-binary-tree-from-preorder-and-inorder-traversal/bus710.go new file mode 100644 index 000000000..de9d2067d --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/bus710.go @@ -0,0 +1,105 @@ +package hello + +import ( + "errors" + "log" +) + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +func buildTree(preorder []int, inorder []int) *TreeNode { + if len(preorder) == 1 { + return &TreeNode{ + Val: preorder[0], + } + } + + center, _ := findCenterWithValue(preorder[0], inorder) + + head := TreeNode{ + Val: inorder[center], + } + + execute(&head, center, inorder) + + return &head +} + +func findCenterWithValue(key int, inorder []int) (int, error) { + for i, n := range inorder { + if n == key { + return i, nil + } + } + return 0, errors.New("not found") +} + +func findCenter(slice []int) int { + c := len(slice) / 2 + return c +} + +func getSplit(center int, inorder []int, currentVal int) ([]int, []int) { + if len(inorder) == 1 { + if inorder[0] == currentVal { + return nil, nil + } + return []int{inorder[0]}, nil + } + if len(inorder) == 2 { + if inorder[0] == currentVal { + return nil, []int{inorder[1]} + } + if inorder[1] == currentVal { + return []int{inorder[0]}, nil + } + return []int{inorder[0]}, []int{inorder[1]} + } + return inorder[:center], inorder[center+1:] +} + +func execute(currentNode *TreeNode, center int, slice []int) { + left, right := getSplit(center, slice, currentNode.Val) + + if len(left) == 1 { + currentNode.Left = &TreeNode{Val: left[0]} + } else if len(left) == 2 { + currentNode.Left = &TreeNode{Val: left[0]} + currentNode.Left.Right = &TreeNode{Val: left[1]} + } else if len(left) > 2 { + lc := findCenter(left) + currentNode.Left = &TreeNode{Val: left[lc]} + + if len(left) > 2 { + execute(currentNode.Left, lc, left) + } + } + + if len(right) == 1 { + currentNode.Right = &TreeNode{Val: right[0]} + } else if len(right) == 2 { + currentNode.Right = &TreeNode{Val: right[0]} + currentNode.Right.Left = &TreeNode{Val: right[1]} + } else if len(right) > 2 { + rc := findCenter(right) + currentNode.Right = &TreeNode{Val: right[rc]} + + if len(right) > 2 { + execute(currentNode.Right, rc, right) + } + } +} + +func print(currentNode *TreeNode) { + log.Println(currentNode.Val) + if currentNode.Left != nil { + print(currentNode.Left) + } + if currentNode.Right != nil { + print(currentNode.Right) + } +} diff --git a/counting-bits/bus710.rs b/counting-bits/bus710.rs new file mode 100644 index 000000000..b6a28dc28 --- /dev/null +++ b/counting-bits/bus710.rs @@ -0,0 +1,34 @@ +// T: O(n log n) +// S: O(n) + +pub fn count_bits(n: i32) -> Vec { + // Prepare a vector. + // The first item can just be 0 + let mut vec = Vec::from([0]); + + // Iterate as many as the given number + 1 + for num in 1..n + 1 { + // Get a binary string from the number (ex: 2 => 10) + let num_str = format!("{num:b}"); + // Count '1' from the given binary string + let cnt = num_str.chars().filter(|c| *c == '1').count(); + // Store the number in the vector + vec.push(cnt as i32); + } + + vec +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = count_bits(2); + assert_eq!(result, Vec::from([0, 1, 1])); + + let result2 = count_bits(5); + assert_eq!(result2, Vec::from([0, 1, 1, 2, 1, 2])); + } +} diff --git a/valid-anagram/bus710.rs b/valid-anagram/bus710.rs new file mode 100644 index 000000000..88d53d2d3 --- /dev/null +++ b/valid-anagram/bus710.rs @@ -0,0 +1,56 @@ +// Space complexity: O(2n) - 주어진 한 단어안의 각 문자가 서로 다 다를 경우 생성한 맵들의 최대 길이는 주어진 단어만큼이므로 2n +// Time complexity: O(3n) - 각 맵을 생성하고 추가로 각 아이템을 비교하는 루프가 필요하므로 3n + +use std::collections::HashMap; + +pub fn is_anagram(s: String, t: String) -> bool { + // Check if the lengh of the 2 words are same. + // Otherwise return false. + let len01 = s.len(); + let len02 = t.len(); + if len01 != len02 { + return false; + } + + // Prepare and fill a new map for s + let mut s_map = HashMap::new(); + for s_char in s.chars() { + let n = s_map.get(&s_char).copied().unwrap_or(0); + s_map.insert(s_char, n + 1); + } + + // Prepare and fill a new map for t + let mut t_map = HashMap::new(); + for t_char in t.chars() { + let n = t_map.get(&t_char).copied().unwrap_or(0); + t_map.insert(t_char, n + 1); + } + + // Iterate over the map s, so compare with the map t + // to see if both have same number for the same character respectively + for (s_char, num) in s_map.iter() { + if t_map.get(s_char).copied().unwrap_or(0) != *num { + return false; + } + } + + true +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_good() { + let result = is_anagram("ana".to_owned(), "aan".to_owned()); + assert!(result); + } + + #[test] + fn test_bad() { + let result = is_anagram("aaa".to_owned(), "aan".to_owned()); + assert!(!result); + } +} +