1003. Check If Word Is Valid After Substitutions

We are given that the string "abc" is valid.

From any valid string V, we may split V into two pieces X and Y such that X + Y (X concatenated with Y) is equal to V.  (X or Y may be empty.)  Then, X + "abc" + Y is also valid.

If for example S = "abc", then examples of valid strings are: "abc", "aabcbc", "abcabc", "abcabcababcc".  Examples of invalid strings are: "abccba""ab""cababc""bac".

Return true if and only if the given string S is valid.

 

Example 1:

Input: "aabcbc"
Output: true
Explanation: 
We start with the valid string "abc".
Then we can insert another "abc" between "a" and "bc", resulting in "a" + "abc" + "bc" which is "aabcbc".

Example 2:

Input: "abcabcababcc"
Output: true
Explanation: 
"abcabcabc" is valid after consecutive insertings of "abc".
Then we can insert "abc" before the last letter, resulting in "abcabcab" + "abc" + "c" which is "abcabcababcc".

Example 3:

Input: "abccba"
Output: false

Example 4:

Input: "cababc"
Output: false

Note:

  1. 1 <= S.length <= 20000
  2. S[i] is 'a''b', or 'c'

题意:设"abc"为一个合法的字符串。对于任意合法串V,我们可以将其分为X,Y两部分,其中X+Y=V。则X+"abc"+Y也是一个合法的字符串。给出一个字符串,问是否合法。

方法一:使用了string的find函数和erase函数,当串中包含"abc"子串时,将其删除,最后判断串是否为空即可。

class Solution {
public:
    stack<char>st;
    bool isValid(string S) {
        int flag=1,pos;
        while(S!=""&&flag==1){
               flag=0;
               //pos=S.find(t) 在S中查找子串t,存在返回其开始位置,没有则返回-1 
               if((pos=S.find("abc"))!=-1){
                   flag=1;
                   //S.erase(pos,len) 从pos开始,删除len个字母 
                   S.erase(pos,3);
               }
        }
        if(S=="")
               return 1;
        else 
            return 0;
    }
};

方法二:借助栈实现。根据题意可知,每个字符串都可以看作是由若干个"abc"子串在保持原顺序的情况下插入其他合法子串得到的。将未匹配的字符入栈,当前元素为c时,则栈中最后两个元素应该为'a'和'b',此时将它们出栈,继续遍历下一元素即可。若不为'a'和'b',则由该串不合法。

class Solution {
public:
      bool isValid(string S) {
        vector<char> stack;
        for (char c : S) {
            if (c == 'c') {
                int n = stack.size();
                if (n < 2 || stack[n - 1] != 'b' || stack[n - 2] != 'a') return false;
                stack.pop_back(), stack.pop_back();
            } else {
                stack.push_back(c);
            }
        }
        return stack.size() == 0;
    }
};

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I can help you with that. Here's a Python function that implements the autocorrect algorithm based on the input parameters: ``` def autocorrect(user_word, valid_words, diff_function, limit): if user_word in valid_words: return user_word min_diff = limit + 1 min_word = user_word for word in valid_words: diff = diff_function(user_word, word, limit) if diff < min_diff: min_diff = diff min_word = word elif diff == min_diff and valid_words.index(word) < valid_words.index(min_word): min_word = word if min_diff <= limit: return min_word else: return user_word ``` The function takes in the user_word, valid_words list, a diff_function, and a limit. The first if statement checks if the user_word is already in the valid_words list and returns it if so. Otherwise, it initializes the minimum difference and minimum word variables to the user_word. The function then iterates over all words in the valid_words list and calculates the difference between the user_word and each word using the provided diff_function. If the difference is less than the current minimum difference, the minimum difference and minimum word variables are updated. If the difference is equal to the minimum difference, the function returns the first word in the valid_words list that has that minimum difference. Finally, the function checks if the minimum difference is less than or equal to the limit and returns the minimum word if so. Otherwise, it returns the user_word. You can define your own diff_function based on your requirements. Here's an example diff_function that calculates the Levenshtein distance between two strings: ``` def levenshtein_distance(s1, s2, limit): if abs(len(s1) - len(s2)) > limit: return limit + 1 if s1 == s2: return 0 if len(s1) > len(s2): s1, s2 = s2, s1 prev_row = list(range(len(s1) + 1)) for i, c2 in enumerate(s2): curr_row = [i + 1] + [0] * len(s1) for j, c1 in enumerate(s1): insertions = prev_row[j + 1] + 1 deletions = curr_row[j] + 1 substitutions = prev_row[j] + (c1 != c2) curr_row[j + 1] = min(insertions, deletions, substitutions) prev_row = curr_row return prev_row[-1] ``` This function takes in two strings, s1 and s2, as well as the limit. It calculates the Levenshtein distance between the two strings and returns it if it is less than or equal to the limit. If the difference is greater than the limit, the function returns the limit plus one. You can call the autocorrect function with your own user_word, valid_words list, and diff_function. For example: ``` >>> user_word = "speling" >>> valid_words = ["spelling", "speaking", "swimming", "smiling"] >>> limit = 2 >>> autocorrect(user_word, valid_words, levenshtein_distance, limit) 'spelling' ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值