# ValidString

* There are 3 rules for a valid string:
  1. An empty string is valid
  2. You can add same character to a valid string X, and create another valid string yXy
  3. You can concatenate two valid strings X and Y, so XY will also be valid.
  4. Ex: vv, xbbx, bbccdd, xyffyxdd are all valid..\
     (It's essentially the valid parentheses question but with alphabets instead of parentheses <https://leetcode.com/problems/valid-parentheses/>. This can be solve in `O(n)` with a stack.)

     Using stack - <https://leetcode.com/playground/MYxTQJdX>

     ```
     static boolean isValid(String s) {
             Stack<Character> st =  new Stack<>();
             for(char c : s.toCharArray()){
                 if(st.isEmpty()){
                     st.push(c);
                 }else if(st.peek() == c){
                     st.pop();
                 }else{
                     st.push(c);
                 }
             }
             return st.isEmpty();
         }
     ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://junnie.gitbook.io/amazon-mock-interview/2022/oa/validstring.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
