Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. // String Compression: Implement a method to perform basic string
  2. // compression using the counts of repeated characters. For example,
  3. // the string aabcccccaaa would become a2b1c5a3. If the "compressed"
  4. // string would not become smaller than the original string, your
  5. // method should return the original string. You can assume the string
  6. // has only uppercase and lowercase letters (a -z).
  7.  
  8. // number always required after string?
  9. // if only one letter, always add a 1 after it?
  10.  
  11. // How big might these strings be?
  12. // How many times in a row might a char occur?
  13.  
  14. // are we receiving lots of strings that don't need to be
  15. // "compressed"? If so, then we should optimize for not having to
  16. // create the compressed string up front.
  17.  
  18. use arrayvec::ArrayString;
  19.  
  20. // calculates the string length beforehand
  21. fn getCompressedLength(s: &str) -> i32 {
  22. let mut prev = None;
  23. let mut count = 0;
  24. let mut length = 0;
  25. for c in s.chars() {
  26. if let Some(temp) = prev {
  27. if temp == c {
  28. count += 1;
  29. } else {
  30. length += 1; // for the char
  31. length += (count % 10) + 1; // for the base 10 count following the char
  32. }
  33. } else {
  34. prev = Some(c)
  35. }
  36. }
  37. if let Some(temp) = prev {
  38. length += 1;
  39. length += (count % 10) + 1;
  40. }
  41. length
  42. }
  43.  
  44. fn compress(s: &str) -> String {
  45. // let mut compressed: Vec<char> = Vec::new();
  46.  
  47. let length = getCompressedLength(s);
  48. // let mut compressed: [char; length];
  49. let mut compressed = ArrayString::<[u8; length]>::new();
  50.  
  51. let mut count = 0;
  52. let mut prev: Option<char> = None;
  53. for c in s.chars() {
  54. if let Some(inner_prev) = prev {
  55. if c == inner_prev {
  56. count += 1;
  57. } else {
  58. compressed.push(inner_prev);
  59. count.to_string().chars().for_each(|ic| compressed.push(ic));
  60. // reset our counts to the current c:
  61. count = 1;
  62. prev = Some(c);
  63. }
  64. } else {
  65. prev = Some(c);
  66. count = 1;
  67. }
  68. }
  69. if let Some(unwrapped_c) = prev {
  70. compressed.push(unwrapped_c);
  71. count.to_string().chars().for_each(|ic| compressed.push(ic));
  72. }
  73.  
  74. return if compressed.len() > s.len() {
  75. s.to_string()
  76. } else {
  77. compressed.into_iter().collect()
  78. };
  79. }
  80.  
  81. #[cfg(test)]
  82. mod tests {
  83. use super::*;
  84.  
  85. #[test]
  86. fn test_1() {
  87. assert_eq!(compress("aabcccccaaa"), "a2b1c5a3");
  88. assert_eq!(compress("abcaa"), "abcaa");
  89. assert_eq!(compress("doooonnkeee"), "d1o4n2k1e3");
  90. }
  91. }
  92.  
  93. fn main() {
  94. compress("doooonnkeee");
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement