In this challenge, you will implement a finite state automaton (FSA) to recognize a specific pattern in a sequence of characters.
A finite state automaton is a mathematical model of computation used to design both computer programs and sequential logic circuits. It is a powerful tool for pattern matching as it consists of a finite number of states and transitions between these states based on input symbols.
Finite state automatons are widely used in text processing, lexical analysis, and many other areas where pattern recognition is essential.
You need to create an FSA that can recognize the pattern "ab*c", where:
You will implement a function recognize_pattern
that takes a string slice as input and returns a boolean indicating whether the input string matches the pattern.
match
statement.match
statement.let result = recognize_pattern("abbbc");
assert_eq!(result, true);
let result = recognize_pattern("ac");
assert_eq!(result, true);
let result = recognize_pattern("abbbd");
assert_eq!(result, false);
let result = recognize_pattern("");
assert_eq!(result, false);
Did You Know?
Finite state automatons have a wide range of applications outside computer science as well. For example, they are used in the design of digital circuits. In digital circuit design, an FSA can be used to create sequential circuits such as counters and communication protocol controllers. FSAs are also used in the field of linguistics to model the morphology of languages and in robotics to control the behavior of autonomous robots.
#[derive(PartialEq)]pub enum StateMachine { Initial, Final, Intermediate,}pub fn recognize_pattern(input: &str) -> bool { let mut state = StateMachine::Initial; for c in input.chars() { match state { StateMachine::Initial => match c { 'a' => state = StateMachine::Intermediate, _ => return false, }, StateMachine::Intermediate => match c { 'b' => state = StateMachine::Intermediate, 'c' => state = StateMachine::Final, _ => return false, }, StateMachine::Final => match c { _ => return false, }, } } state == StateMachine::Final}
#[derive(PartialEq)]pub enum StateMachine { Initial, Final, Intermediate,}pub fn recognize_pattern(input: &str) -> bool { let mut state = StateMachine::Initial; for c in input.chars() { match state { StateMachine::Initial => match c { 'a' => state = StateMachine::Intermediate, _ => return false, }, StateMachine::Intermediate => match c { 'b' => state = StateMachine::Intermediate, 'c' => state = StateMachine::Final, _ => return false, }, StateMachine::Final => match c { _ => return false, }, } } state == StateMachine::Final}
pub fn recognize_pattern(input: &str) -> bool { let mut current_state = FSA::STATEA; for c in input.chars() { current_state = current_state.transition(c); } current_state.is_end_state()}pub enum FSA { STATEA, STATEBs, STATEC, ERROR,}impl FSA { pub fn transition(&self, symbol: char) -> FSA { match (&self, symbol) { (FSA::STATEA, 'a') => FSA::STATEBs, (FSA::STATEBs, 'b') => FSA::STATEBs, (FSA::STATEBs, 'c') => FSA::STATEC, _ => FSA::ERROR, } } pub fn is_end_state(&self) -> bool { match self{ FSA::STATEC => return true, _ => return false, } }}
pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut chars = input.chars(); let mut current_state: State = match chars.next() { None => { return false; } Some(char) => State::from(char), }; for char in chars { let next_state = State::from(char); if !current_state.available_states().contains(&next_state) { return false; } current_state = next_state; } match current_state { State::C => true, _ => false, }}#[derive(PartialEq, PartialOrd)]enum State { A, B, C, Other,}impl From<char> for State { fn from(value: char) -> Self { match value { 'a' => Self::A, 'b' => Self::B, 'c' => Self::C, _ => Self::Other, } }}impl State { pub fn available_states(&self) -> Vec<State> { match self { Self::A => vec![Self::B, Self::C], Self::B => vec![Self::B, Self::C], Self::C => vec![], Self::Other => vec![], } }}
pub fn recognize_pattern(input: &str) -> bool { if input.is_empty() { return false; } let mut chars = input.chars().into_iter(); match chars.next() { Some('a') => (), _ => return false, } loop { match chars.next() { Some('b') => continue, Some('c') => break, _ => return false, } } if chars.next().is_none() { true } else { false }}
pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut iter = input.chars(); match iter.next(){ Some('a') => (), _ => return false } loop{ match iter.next(){ Some('b') => continue, Some('c') => break, _ => return false } } if iter.next() == None { return true } false}
pub fn recognize_pattern(input: &str) -> bool { if input.is_empty() { return false } let mut chars = input.chars(); let mut current: State = chars.next().unwrap().into(); for c in chars { let s: State = c.into(); let possible = current.transition(); if !possible.contains(&s) { return false } current = s; } if current == State::C { true } else { false }}#[derive(PartialEq, Eq, Debug)]enum State { A, B, C, Other}impl From<char> for State { fn from(value: char) -> Self { match value { 'a' => State::A, 'b' => State::B, 'c' => State::C, _ => State::Other } }}impl State { fn transition(&self) -> Vec<State> { match self { State::A => vec![State::B, State::C], State::B => vec![State::B, State::C], State::C => vec![], State::Other => vec![State::A, State::Other], } }}
pub fn recognize_pattern(input: &str) -> bool { if input.is_empty() { return false } let mut chars = input.chars(); let mut current: State = chars.next().unwrap().into(); for c in chars { let s: State = c.into(); let possible = current.transition(); if !possible.contains(&s) { return false } current = s; } if current == State::C { true } else { false }}#[derive(PartialEq, Eq, Debug)]enum State { A, B, C, Other}impl From<char> for State { fn from(value: char) -> Self { match value { 'a' => State::A, 'b' => State::B, 'c' => State::C, _ => State::Other } }}impl State { fn transition(&self) -> Vec<State> { match self { State::A => vec![State::B, State::C], State::B => vec![State::B, State::C], State::C => vec![], State::Other => vec![State::A, State::Other], } }}
pub fn recognize_pattern(input: &str) -> bool { if input.is_empty() { return false } let mut chars = input.chars(); let mut current: State = chars.next().unwrap().into(); for c in chars { let s: State = c.into(); let possible = current.transition(); if !possible.contains(&s) { return false } current = s; } if current == State::C { true } else { false }}#[derive(PartialEq, Eq, Debug)]enum State { A, B, C, Other}impl From<char> for State { fn from(value: char) -> Self { match value { 'a' => State::A, 'b' => State::B, 'c' => State::C, _ => State::Other } }}impl State { fn transition(&self) -> Vec<State> { match self { State::A => vec![State::B, State::C], State::B => vec![State::B, State::C], State::C => vec![], State::Other => vec![State::A, State::Other], } }}
pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut s = input.chars(); if input.len() < 2 { return false; } else { match s.next() { Some(c) => if c != 'a' { return false; }, None => return false, }; match s.last() { Some(c) => if c != 'c' { return false; }, None => return false, } let chars = input.chars().skip(1); for (i, c) in chars.enumerate() { if i < input.len() - 2 && c != 'b' { return false; } } } true}
pub enum LetterState { IsA, IsB, IsC, Invalid,}pub fn get_state(c: &char) -> LetterState { match c { 'a' => LetterState::IsA, 'b' => LetterState::IsB, 'c' => LetterState::IsC, _ => LetterState::Invalid, }}pub struct Letter { pub value: LetterState}impl Letter { pub fn new(c: char) -> Self { Self { value: get_state(&c) } } pub fn transition(&mut self, next: &Letter) { match self.value { LetterState::IsA => { match next.value { LetterState::IsA => self.value = LetterState::Invalid, LetterState::IsB => self.value = LetterState::IsB, LetterState::IsC => self.value = LetterState::IsC, LetterState::Invalid => self.value = LetterState::Invalid, } }, LetterState::IsB => { match next.value { LetterState::IsA => self.value = LetterState::Invalid, LetterState::IsB => (), LetterState::IsC => self.value = LetterState::IsC, LetterState::Invalid => self.value = LetterState::Invalid, } }, LetterState::IsC => self.value = LetterState::Invalid, LetterState::Invalid => self.value = LetterState::Invalid, } }}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut chars = input.chars(); if let Some(current_char) = chars.next() { let mut current = Letter::new(current_char); while let Some(c) = chars.next() { current.transition(&Letter::new(c)); match current.value { LetterState::IsA | LetterState::IsB | LetterState::IsC => (), LetterState::Invalid => return false, } } match current.value { LetterState::IsA | LetterState::IsB | LetterState::Invalid => return false, LetterState::IsC => return true, } } else { return false; }}
#[derive(Debug)]enum State { Start, SeenA, SeenB, SeenC, Invalid,}fn transition(state: State, input: char) -> State { match state { State::Start => match input { 'a' => State::SeenA, _ => State::Invalid, }, State::SeenA => match input { 'b' => State::SeenB, 'c' => State::SeenC, _ => State::Invalid, }, State::SeenB => match input { 'b' => State::SeenB, 'c' => State::SeenC, _ => State::Invalid, }, State::SeenC => State::Invalid, State::Invalid => State::Invalid, }}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Start; for c in input.chars() { state = transition(state, c); if let State::Invalid = state { return false; } } matches!(state, State::SeenC)}
pub enum States { Start, SeenA, SeenB, SeenC,}pub struct Machine { state: States}impl Machine { pub fn new() -> Self { Self { state: States::Start } } pub fn start(&mut self) { self.state = States::Start } pub fn a(&mut self) { match self.state { _ => self.state = States::SeenA, } } pub fn b(&mut self) { match self.state { States::Start => (), States::SeenC => self.state = States::Start, _ => self.state = States::SeenB, } } pub fn c(&mut self) { match self.state { States::Start => (), States::SeenA => self.state = States::SeenC, States::SeenB => self.state = States::SeenC, _ => self.state = States::Start } } pub fn pattern(&self) -> bool { match self.state { States::SeenC => true, _ => false, } } pub fn find_pattern(&mut self, pat: &str) -> bool { for ch in pat.chars() { match ch { 'a' => self.a(), 'b' => self.b(), 'c' => self.c(), _ => self.start(), } } if self.pattern() { return true; } return false; }}pub fn recognize_pattern(input: &str) -> bool { let mut machine = Machine::new(); println!("{input}"); machine.find_pattern(input)}
pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Initial; for ch in input.chars() { let next_state = accept(&state, ch); match next_state { State::Failed => { return false; } _ => { state = next_state; } } } match state { State::Found => {true}, _ => {false} }}fn accept(state: &State, next_char: char) -> State { match state { State::Initial => { if next_char == 'a' {State::FoundA} else {State::Failed} } State::FoundA => { if next_char == 'b' {State::FoundB} else if next_char == 'c' {State::Found} else {State::Failed} } State::FoundB => { if next_char == 'b' {State::FoundB} else if next_char == 'c' {State::Found} else {State::Failed} } State::Found => State::Failed, State::Failed => State::Failed }}enum State { Initial, FoundA, FoundB, Found, Failed,}
#[derive(PartialEq)]enum State { Start, A, B, C} pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = State::Start; for ch in input.chars() { state = match state { State::Start => match ch { 'a' => State::A, _ => return false, }, State::A => match ch { 'b' => State::B, 'c' => State::C, _ => return false, }, State::B => match ch { 'b' => State::B, 'c' => State::C, _ => return false, }, State::C => match ch { _ => return false, }, } } state == State::C}
enum State { Start, FoundA, FoundB, FoundC, Rejected}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Start; for c in input.chars() { state = match state { State::Start => { match c { 'a' => State::FoundA, _ => State::Rejected } }, State::FoundA => { match c { 'b' => State::FoundB, 'c' => State::FoundC, _ => State::Rejected } }, State::FoundB => { match c { 'b' => State::FoundB, 'c' => State::FoundC, _ => State::Rejected } }, State::FoundC => State::Rejected, State::Rejected => State::Rejected } } matches!(state, State::FoundC)}
#[derive(PartialEq)]enum State { Start, OnA, OnB, Match, Invalid,}impl State { fn transition(&self, c: char) -> Self { match (self, c) { (State::Start, 'a') => State::OnA, (State::OnA, 'b') => State::OnB, (State::OnA, 'c') => State::Match, (State::OnB, 'b') => State::OnB, (State::OnB, 'c') => State::Match, (_, _) => State::Invalid, } }}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Start; for c in input.chars() { state = state.transition(c); if state == State::Invalid { break; } } state == State::Match}
#[derive(PartialEq)]enum State { Start, OneA, ZeroOrMoreB, OneC, Finish,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here if input.is_empty() { return false; } let mut state = State::Start; let mut chars = input.chars().peekable(); while state != State::Finish { match state { State::Start => { if let Some(&ch) = chars.peek() { if ch == 'a' { chars.next(); state = State::OneA; } else { return false; } } else { return false; } } State::OneA | State::ZeroOrMoreB => { if let Some(&ch) = chars.peek() { if ch == 'c' { chars.next(); state = State::OneC; } else if ch == 'b' { chars.next(); state = State::ZeroOrMoreB; } else { return false; } } else { return false; } } State::OneC => { if let Some(&_ch) = chars.peek() { return false; } else { state = State::Finish; } } _ => { break; } } } true}
#[derive(PartialEq)]enum State { Start, FoundA, FoundC, Invalid,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = State::Start; for c in input.chars() { state = match state { State::Start => { match c { 'a' => State::FoundA, _ => State::Invalid, } }, State::FoundA => { match c { 'b' => State::FoundA, 'c' => State::FoundC, _ => State::Invalid, } }, State::FoundC => State::Invalid, State::Invalid => State::Invalid, }; } state == State::FoundC}
pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here enum State{ Start, StateA, StateB, Reject, Accept, } impl State{ fn transition(&self, c:char) -> State{ match (self,c){ (Self::Start, 'a') => Self::StateA, (Self::StateA, 'b') => Self::StateB, (Self::StateA, 'c') => Self::Accept, (Self::StateB, 'b') => Self::StateB, (Self::StateB, 'c') => Self::Accept, (_,_) => State::Reject, } } } let mut state = State::Start; for character in input.chars(){ state = State::transition(&state,character); if let State::Reject = state{ return false; } } if let State::Accept =state{ return true; } false}
#[derive(PartialEq)]enum FSA { NONE, CORRECT}pub fn recognize_pattern(input: &str) -> bool { if input.len() < 2 { return false; } let left = input.chars().next(); let right = input.chars().last(); let mut middle_state: FSA = FSA::CORRECT; if left != Some('a') || right != Some('c') { return false; } let middle: String = input.chars().skip(1).take(input.len() - 2).collect(); for ch in middle.chars() { if ch != 'b' { middle_state = FSA::NONE; break; } } if (middle_state == FSA::CORRECT) { return true; } else { return false; }}
pub fn recognize_pattern(input: &str) -> bool { let mut state = State::A; for ch in input.chars() { match state { State::A if ch == 'a' => state = State::B, State::A => return false, State::B if ch == 'c' => state = State::EOL, State::B if ch == 'b' => state = State::B, State::B => return false, State::EOL if ch != '\n' => return false, State::EOL => return true, }; } state == State::EOL}#[derive(PartialEq, Eq)]enum State { A, B, EOL,}
#[derive(PartialEq)]pub enum State { A, BS, C}pub fn recognize_pattern(input: &str) -> bool { let mut chars: Vec<char> = input.chars().collect(); let mut state: State = State::A; while chars.len() != 0 { let current = chars.remove(0); match (state, current) { (State::A, 'a') => state = State::BS, (State::BS, 'b') => state = State::BS, (State::BS, 'c') => state = State::C, _ => { return false } } } if state == State::C { return true; } else { return false; }}
#[derive(PartialEq)]enum State { Start, A, B, C}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = State::Start; for ch in input.chars() { state = match state { State::Start => match ch { 'a' => State::A, _ => return false, }, State::A => match ch { 'b' => State::B, 'c' => State::C, _ => return false, }, State::B => match ch { 'b' => State::B, 'c' => State::C, _ => return false, }, State::C => match ch { _ => return false, }, } } state == State::C}
#[derive(PartialEq)]enum State { Start, A, B, C,}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Start; for c in input.chars() { state = match state { State::Start => match c { 'a' => State::A, _ => return false, }, State::A => match c { 'b' => State::B, 'c' => State::C, _ => return false, }, State::B => match c { 'b' => State::B, 'c' => State::C, _ => return false, }, State::C => return match c { _ => false, }, }; } state == State::C}
#[derive(PartialEq)]enum State { Start, A, B, C,}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Start; for c in input.chars() { state = match state { State::Start => match c { 'a' => State::A, _ => return false, }, State::A => match c { 'b' => State::B, 'c' => State::C, _ => return false, }, State::B => match c { 'b' => State::B, 'c' => State::C, _ => return false, }, State::C => match c { _ => return false, }, }; } let result = state == State::C; return result;}
#[derive(PartialEq)]enum State { Init, A, B, C,}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Init; input.chars().all(|c| match (&state, c) { (State::Init, 'a') => { state = State::A; true } (State::A | State::B, 'b') => { state = State::B; true } (State::A | State::B, 'c') => { state = State::C; true } _ => false, }) && state == State::C}
#[derive(Eq, PartialEq)]enum State { Init, A, B, C,}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Init; input.chars().all(|c| match (&mut state, c) { (State::Init, 'a') => { state = State::A; true } (State::A | State::B, 'b') => { state = State::B; true } (State::A | State::B, 'c') => { state = State::C; true } _ => false, }) && state == State::C}
#[derive(PartialEq)]pub enum State { Start, A, AB, Invalid, Match,}pub enum Event { Char(char),}pub struct FSA { state: State}impl FSA { pub fn new() -> Self { FSA { state: State::Start, } } pub fn transition(&mut self, event: Event) { self.state = match (&self.state, event) { (State::Start, Event::Char('a')) => State::A, (State::A, Event::Char('b')) => State::AB, (State::AB, Event::Char('b')) => State::AB, (State::A, Event::Char('c')) | (State::AB, Event::Char('c')) => State::Match, (State::Match, Event::Char(_)) | (_, _) => State::Invalid, }; } pub fn is_match(&self) -> bool { self.state == State::Match } pub fn is_invalid(&self) -> bool { self.state == State::Invalid }}pub fn recognize_pattern(input: &str) -> bool { let mut fsa = FSA::new(); for c in input.chars() { fsa.transition(Event::Char(c)); if fsa.is_invalid() { return false; } } fsa.is_match()}
enum State { A, B, C,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = State::A; let mut found = false; for char in input.chars() { match state { State::A => { if char != 'a' { return false } state = State::B; } State::B => { if char == 'b' { continue; } if char == 'c' { state = State::C; found = true; continue; } return false; } State::C => { found = false; } } } found}
enum DFAState { Invalid, Start, MatchABStar, MatchABStarC,}pub fn recognize_pattern(input: &str) -> bool { let mut state = DFAState::Start; for c in input.chars() { state = match state { DFAState::Start => match c { 'a' => DFAState::MatchABStar, _ => DFAState::Invalid, }, DFAState::Invalid => match c { _ => DFAState::Invalid, }, DFAState::MatchABStar => match c { 'b' => DFAState::MatchABStar, 'c' => DFAState::MatchABStarC, _ => DFAState::Invalid, }, DFAState::MatchABStarC => match c { _ => DFAState::Invalid, }, } } match state { DFAState::MatchABStarC => true, _ => false, }}
pub fn recognize_pattern(input: &str) -> bool { let lenn = input.len(); if input.chars().nth(0) != Some('a') { return false; } if input.chars().nth(lenn-1) != Some('c') { return false; } let mut i = 1; while i < lenn-1 { if input.chars().nth(i) != Some('b') { return false; } i+=1; } true}
pub fn recognize_pattern(input: &str) -> bool { let mut state = State::A; for ch in input.chars() { match state { State::A if ch == 'a' => state = State::B, State::A => return false, State::B if ch == 'c' => state = State::EOL, State::B if ch == 'b' => (), State::B => return false, State::EOL if ch != '\n' => return false, State::EOL => (), }; } state == State::EOL}#[derive(PartialEq, Eq)]enum State { A, // Need to find 1 A B, // Need to find a number of B and then a C EOL, // Need to find the input end.}
pub fn recognize_pattern(input: &str) -> bool { let mut state = State::A; for ch in input.chars() { match state { State::A if ch == 'a' => state = State::B, State::A => return false, State::B if ch == 'c' => state = State::EOL, State::B if ch == 'b' => (), State::B => return false, State::EOL if ch != '\n' => return false, State::EOL => (), }; } state == State::EOL}#[derive(PartialEq, Eq)]enum State { A, // Need to find 1 A B, // Need to find a number of B and then a C EOL, // Need to find the input end.}
pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let lenn = input.len(); if input.chars().nth(0) != Some('a') { return false; } if input.chars().nth(lenn-1) != Some('c') { return false; } let mut i = 1; while i < lenn-1 { if input.chars().nth(i) != Some('b') { return false; } i+=1; } true}
pub enum State{ Init, A, B, C, Error,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here match input.chars().fold(State::Init, |state, c|{ match state { State::Init => if c == 'a' { State::A } else { State::Error }, State::A => if c == 'b' { State::B } else if c == 'c'{ State::C } else { State::Error }, State::B => if c == 'b' { State::B } else if c == 'c'{ State::C } else { State::Error }, State::C => State::Error , State::Error => State::Error, } }) { State::C => true, _ => false, } }
pub fn recognize_pattern(input: &str) -> bool { let mut chars = input.chars(); if chars.next() != Some('a') { return false; } while let Some(value) = &mut chars.next() { if *value == 'b' { continue; } else if *value == 'c' { return None == chars.next(); } } false}
#[derive(Debug, PartialEq)]enum State { Start, A, B, C,}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Start; input.chars().all(|c| match (&mut state, c) { (State::Start, 'a') => { state = State::A; true } (State::A | State::B, 'b') => { state = State::B; true } (State::A | State::B, 'c') => { state = State::C; true } _ => false, }) && state == State::C}
#[derive(PartialEq)]enum States { Initial, FoundA, FoundB, FoundC,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here if input.chars().count() == 0 { return false; } for letter in input.chars() { if !"abc".contains(letter) { return false; } } let mut state = States::Initial; for character in input.chars() { state = fsm(state, &character); } state == States::FoundC}fn fsm(state: States, character: &char) -> States { match (state, character) { (States::Initial, 'a') => States::FoundA, (States::FoundA, 'a') => States::FoundA, (States::FoundA, 'b') => States::FoundB, (States::FoundA, 'c') => States::FoundC, (States::FoundB, 'b') => States::FoundB, (States::FoundB, 'c') => States::FoundC, (_, _) => States::Initial }}
#[derive(Debug, PartialEq)]enum State { Start, A, B, C,}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Start; input.chars().all(|c| match (&mut state, c) { (State::Start, 'a') => { state = State::A; true } (State::A | State::B, 'b') => { state = State::B; true } (State::A | State::B, 'c') => { state = State::C; true } _ => false, }) && state == State::C}
#[derive(Debug, PartialEq)]enum States { Init, GotA, GotB, GotC,}fn fsm(state: States, c: char) -> States { println!("{state:?} {c}"); match (state, c) { (States::Init, 'a') => States::GotA, (States::GotA, 'a') => States::GotA, (States::GotA, 'b') => States::GotB, (States::GotA, 'c') => States::GotC, (States::GotB, 'b') => States::GotB, (States::GotB, 'c') => States::GotC, (States::GotC, _) => if c != 'c' { States::GotC } else { States::Init }, (_, _) => States::Init, }}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here println!("pattern: {input}"); if input.len() > 0 { let mut state = States::Init; for c in input.chars() { state = fsm(state, c); } if state == States::GotC { return true; } } false}
pub fn recognize_pattern(input: &str) -> bool { let mut matcher = Matcher::new(); matcher.matches(input)}#[derive(Debug, Eq, PartialEq)]enum FSA { Start, A, Bs, CDone, Invalid,}struct Matcher { state: FSA,}impl Matcher { fn new() -> Self { Self { state: FSA::Start, } } fn matches(&mut self, s: &str) -> bool { for c in s.chars() { self.next(c); } self.state == FSA::CDone } fn next(&mut self, c: char) { match self.state { FSA::Start => { match c { 'a' => self.state = FSA::A, _ => self.state = FSA::Start, } }, FSA::A => { match c { 'b' => self.state = FSA::Bs, 'c' => self.state = FSA::CDone, _ => self.state = FSA::Invalid, } }, FSA::Bs => { match c { 'b' => self.state = FSA::Bs, 'c' => self.state = FSA::CDone, _ => self.state = FSA::Invalid, } }, FSA::CDone => { match c { _ => self.state = FSA::Invalid, } }, FSA::Invalid => {} } }}
enum State { Invalid, A, B, C,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let final_state = input.chars().fold(State::Invalid, |state, c| { match (state, c) { (_, 'a') => State::A, (State::A, 'b') => State::B, (State::A, 'c') => State::C, (State::B, 'b') => State::B, (State::B, 'c') => State::C, _ => State::Invalid, } }); matches!(final_state, State::C)}
enum State { Invalid, A, B, C,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let final_state = input.chars().fold(State::Invalid, |state, c| { match (state, c) { (_, 'a') => State::A, (State::A, 'b') => State::B, (State::A, 'c') => State::C, (State::B, 'b') => State::B, (State::B, 'c') => State::C, _ => State::Invalid, } }); matches!(final_state, State::C)}
enum State { S, B, T, Invalid,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = State::S; for x in input.chars() { state = match (state, x) { (State::S, 'a') => State::B, (State::B, 'b') => State::B, (State::B, 'c') => State::T, (_, _) => State::Invalid, }; } match state { State::T => true, _ => false, }}
enum State { S, B, T, Invalid,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = State::S; for x in input.chars() { state = match (state, x) { (State::S, 'a') => State::B, (State::B, 'b') => State::B, (State::B, 'c') => State::T, (_, _) => State::Invalid, }; } matches!(state, State::T)}
enum State { S, B, T, Invalid,}pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = State::S; for x in input.chars() { state = match (state, x) { (State::S, 'a') => State::B, (State::B, 'b') => State::B, (State::B, 'c') => State::T, (_, _) => State::Invalid, }; } match state { State::T => true, _ => false, }}
pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here println!("{}", input); #[derive(PartialEq)] enum States {S, A, B, C} let mut state = States::S; for c in input.chars() { match state { States::S => {if c == 'a' {state = States::A;} else {return false;}}, States::A => {if c == 'b' {state = States::B;} else if c == 'c' {state = States::C;} else {return false;}}, States::B => {if c == 'c' {state = States::C;} else if c == 'b' {continue;} else {return false;}}, States::C => {return false;}, } } if state == States::C { return true; } false}
pub fn recognize_pattern(input: &str) -> bool { // Implement your finite state automaton here let mut state = State::A; for c in input.chars() { match state { State::A => if c == 'a' { state = State::B; } else { break; } State::B => if c == 'b' { continue; } else if c == 'c' { state = State::C; } else { break; } State::C => return false, } } state == State::C}#[derive(PartialEq, Eq)]enum State { A, B, C,}
enum State { Start, Running, End, Invalid,}pub fn recognize_pattern(input: &str) -> bool { let mut state = State::Start; for c in input.chars() { match (state, c) { (State::Start, 'a') => state = State::Running, (State::Running, 'b') => state = State::Running, (State::Running, 'c') => state = State::End, (_, _) => { state = State::Invalid; break; } } } match state { State::End => true, _ => false, }}