1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//! A collection of anagram utility functions
//!
//! ## Installation
//! Add `anagram = 0.3.0` to your Cargo.toml
//!
//! ## Examples
//! ```
//! use anagram::{count, get_next, is_anagram, occurences};
//!
//! fn main() {
//!   // count how many anagrams can be formed from a given word
//!   let anagram_count = count("ordeals");
//!   assert_eq!(anagram_count, 5040);
//!
//!   // count the number of occurences of an anagram in a given word
//!   let occur = occurences("helloworldhello", "ll");
//!   assert_eq!(occur, 2);
//!
//!   // check if a word is an anagram of another word
//!   let ok = is_anagram("rustiscool", "oolcsistru");
//!   assert_eq!(ok, true);
//!
//!   // get the next lexicographically greater anagram
//!   let next = get_next("abcdefg");
//!   assert_eq!(next, "abcdegf");
//!
//!   // get all anagrams of a word
//!   let mut word: String = String::from("abc");
//!   for _ in 0..count(&word) {
//!     // get next anagram
//!     word = get_next(&word);
//!     println!("{}", word);
//!   }
//! }
//! ```
use counter::Counter;
use std::{collections::HashSet, str::from_utf8};

static ASCII_LOWER: [char; 26] = [
  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
  't', 'u', 'v', 'w', 'x', 'y', 'z',
];

fn factorial(n: u128) -> u128 {
  if n <= 1 {
    1
  } else {
    n * factorial(n - 1)
  }
}

/// Count the number of anagrams that can be formed from a word
pub fn count(word: &str) -> u128 {
  let mut unique = HashSet::new();
  let count: usize = word.chars().count();

  for c in word.chars() {
    unique.insert(c);
  }

  if unique.len() == count {
    factorial(count as u128)
  } else {
    let mut seen = HashSet::new();

    let mut divisor: u128 = 1;

    let char_counts = word.chars().collect::<Counter<_>>();

    for c in word.chars() {
      if !seen.contains(&c) {
        divisor *= char_counts[&c] as u128;
        seen.insert(c);
      }
    }

    factorial(count as u128) / divisor
  }
}

/// Count the number of occurences of an anagram in a word
pub fn occurences(word: &str, input: &str) -> u128 {
  let len_word = word.chars().count();
  let len_input = input.chars().count();

  // Check if all counts are zero
  let is_zero = |count: &[i64]| {
    for val in count.iter() {
      if *val != 0 {
        return false;
      }
    }
    true
  };

  let mut count: [i64; 256 as usize] = [0; 256 as usize];

  for val in 0..len_input {
    count[word.as_bytes()[val] as usize] += 1;
  }

  for val in 0..len_input {
    count[input.as_bytes()[val] as usize] -= 1;
  }

  let mut result: u128 = 0;
  result += is_zero(&count) as u128;

  for i in len_input..len_word {
    // add last character
    count[word.as_bytes()[i] as usize] += 1;

    // remove first character
    count[word.as_bytes()[i - len_input] as usize] -= 1;

    result += is_zero(&count) as u128;
  }
  result
}

/// Check if a word is an anagram of another word
pub fn is_anagram(left: &str, right: &str) -> bool {
  if left.chars().count() != right.chars().count() {
    false
  } else {
    let mut count: [i32; 26 as usize] = [0; 26 as usize];

    for c in left.chars() {
      let pos = if let Some(val) = c.to_digit(10) {
        val as usize
      } else {
        ASCII_LOWER
          .iter()
          .position(|&x| x == c.to_lowercase().nth(0).unwrap())
          .unwrap() as usize
      };

      count[pos] += 1;
    }

    for c in right.chars() {
      let pos = if let Some(val) = c.to_digit(10) {
        val as usize
      } else {
        ASCII_LOWER
          .iter()
          .position(|&x| x == c.to_lowercase().nth(0).unwrap())
          .unwrap() as usize
      };

      count[pos] -= 1;

      if count[pos] < 0 {
        return false;
      }
    }

    true
  }
}

/// Get the next lexicographically greater permutation
/// This function will return either the next greater permutation or the
/// lexicographically smallest permutation if the given word is the
/// lexicographically greatest permutation
/// Examples:
/// "abc" -> "acb"
/// "cba" -> "abc"
pub fn get_next(word: &str) -> String {
  let mut i = word.chars().count() - 1;

  // find the first char smaller than the char next to it
  while i > 0 {
    if word.as_bytes()[i] > word.as_bytes()[i - 1] {
      break;
    }
    i -= 1;
  }

  // we are at the lexicographically greatest permutation so we
  // return the lexicographically smallest one
  if i == 0 {
    let mut chars: Vec<char> = word.chars().collect();
    chars.sort();
    return chars.into_iter().collect();
  }

  // find the smallest char on the right side of i-1'th char
  // that's greater than word[i - 1]
  let mut j = i + 1;
  let mut smallest = i;
  let mut word_as_bytes: Vec<u8> = word.to_string().into_bytes();
  while j < word.chars().count() {
    if word_as_bytes[j] > word_as_bytes[i - 1] && word_as_bytes[j] < word_as_bytes[smallest] {
      smallest = j;
    }
    j += 1;
  }

  // swap smallest with word[i - 1]
  word_as_bytes.swap(smallest, i - 1);

  // sort right half
  let mut right_half: Vec<u8> = word_as_bytes[i..word.chars().count()].to_vec();
  right_half.sort();

  // merge back and return as a String
  from_utf8(&[&word_as_bytes[0..i], &right_half].concat())
    .unwrap()
    .to_string()
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_factorial() {
    assert_eq!(factorial(1), 1);
    assert_eq!(factorial(2 * 2), 24);
    assert_eq!(factorial(14), 87178291200);
    assert_eq!(factorial(12), 479001600);
  }

  #[test]
  fn test_anagram_count() {
    assert_eq!(count("at"), 2);
    assert_eq!(count("ordeals"), 5040);
    assert_eq!(
      count("abcdefghijklmnopqrstuvwxyz"),
      403291461126605635584000000
    );
    assert_eq!(count("abcdefghijklmabcdefghijklm"), 49229914688306352000000);
    assert_eq!(count("abcdABCDabcd"), 29937600);
  }

  #[test]
  fn test_is_anagram() {
    assert_eq!(is_anagram("hello", "ooo"), false);
    assert_eq!(is_anagram("Hello", "olleH"), true);
    assert_eq!(is_anagram("hello", "olleh"), true);
    assert_eq!(is_anagram("helicopter", "copterheli"), true);
    assert_eq!(is_anagram("hacker", "hackes"), false);
    assert_eq!(is_anagram("HaCkER", "hacker"), true);
    assert_eq!(is_anagram("ac", "bb"), false);
    assert_eq!(is_anagram("123", "321"), true);
    assert_eq!(is_anagram("1110002293", "1101009322"), true);
    assert_eq!(is_anagram("1102eeaA", "20Svv00"), false);
    assert_eq!(is_anagram("1102eeaA", "0112EEaA"), true);
    assert_eq!(is_anagram("1102eeaA", "0112eaAe"), true);
  }

  #[test]
  fn test_occurences() {
    assert_eq!(occurences("forxxorfxdofr", "for"), 3);
    assert_eq!(occurences("hellohelloleh", "hel"), 3);
    assert_eq!(occurences("oofooflolhi", "oo"), 2);
    assert_eq!(occurences("rustiscool", "st"), 1);
    assert_eq!(occurences("thegrandopeningscenerywasgreat", "grand"), 1);
    assert_eq!(occurences("anagrams", "smargana"), 1);
  }

  #[test]
  fn test_get_next() {
    assert_eq!(get_next("abc"), "acb");
    assert_eq!(get_next("bac"), "bca");
    assert_eq!(get_next("aaa"), "aaa");
    assert_eq!(get_next("cba"), "abc");
    assert_eq!(get_next("218765"), "251678");
    assert_eq!(get_next("1234"), "1243");
    assert_eq!(get_next("4321"), "1234");
    assert_eq!(get_next("534976"), "536479");
  }
}