Sort by

recency

|

93 Discussions

|

  • + 0 comments

    This problem can also be solved by stack

    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    #define SUBSTR_SZ 3 // Substring size
    
    class Stack{
    private:
        vector<string> _stack_vec;
        int 	    _top;
    
    public:
        Stack(){        
            _top = -1;
        }
        void pop(){
            if(!is_empty()) {
                _top = _top - 1;
                _stack_vec.pop_back();
                return;
            } else {
                std::cout << "Could not retrieve data, stack is empty\n";
            }
            return;
        }
        void push(string _str){
            _top += 1;   
            _stack_vec.push_back(_str);
        }
    
        string get_top() {
            return _stack_vec[_top];
        }
        int is_empty(){
            return !_stack_vec.size();
        }
    
        int stack_size() {
            return _top + 1;
        }
    };
    
    int main() {
        Stack stack;
        // string _str = "tactactictactictictac"; // valid
        string _str = "tactictac"; // invalid
    
        // first to label whether the first tic is met
        bool valid = true, first = false;
    
        for (int i = 0; i < _str.size(); i = i+SUBSTR_SZ) {
            string _substr = _str.substr(i, SUBSTR_SZ);
            if (i == 0 && _substr == "tic") {
                valid = false;
                break;
            }
            if (stack.is_empty() && _substr == "tac") {
                stack.push(_substr);
                continue;
            }
    
            if(!stack.is_empty()) {
                if (_substr == "tac") stack.push(_substr);
                else {
                    if (stack.get_top() == "tic") {
                        valid = false;
                        break;
                    } else {
                        // When not meeting the first tac, with current
                        // top is "tac", pop it out
                        if (!first) {
                            stack.pop();
                            // The first tic is met: tac tac tic
                            if (!stack.is_empty() && stack.get_top() == "tac") {
                                first = true;
                                stack.pop();
                                continue;
                            } else valid = false;
                        }
                        else stack.push(_substr);
                    }
                }
            }
        }
    
        std::cout << valid << "\n";
    
       	return 0;
    }
    
  • + 0 comments

    Java 15:

    import java.io.*;
    import java.util.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    public class Solution {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            // Pattern pattern = Pattern.compile("^(tac)(tac(tic)?)*$");    //WORKS
            Pattern pattern = Pattern.compile("^(\\2tic|(tac))+$"); //same as go!go!amigo example
            Matcher matcher = pattern.matcher(scanner.nextLine());
            System.out.println(matcher.find());
        }
    }
    
    /*
    ----------Option 1: "^(tac)(tac(tic)?)*$"--------
    String should not start with tic. So it should start with tac.
    tac can follow either tac or tic.
    tic cannot follow another tic.
    First tic should be after at least two tacs.
    So the regex becomes: Start with tac + Followed by either tac / tactic as many times.
    So tac(tac/tactic)(tac/tactic)(tac/tactic)...
    
    ----------Option 2: "^(\\2tic|(tac))+$"--------
    Break (\\2tic|(tac)) as :
        group_2 = tac
        group_1 = (group_2 and tic) OR (group_2)
    So regex 
        = (\\2tic|(tac))+
        = (group_1)+ 
        = 1 or more repetition of group_1 (Note: group_1 has two alternatives) 
    
    First it will try to match group_2 and tic but still we didn't capture group_2 so we don't get it at first place. 
    So it will match group_2 == tac 
    This will make group_1 == (tactic|tac)
    Then because of + we can match any alternatives from (tactic|tac) more than 1 times.
    This combination is same as the Option 1
    */
    
  • + 1 comment

    ^(\2tic|(tac))+$ in this regex why its getting 2 times the reference of the 2nd group

  • + 1 comment
    $Regex_Pattern = '/^tac((tac)+\2|tac(tic))*$/';
    
  • + 0 comments

    With php be wary of the double escape \ for .