925. 长按键入

 

 

 

 

思路:
i、j指针分别遍历name和typed:
若typed[j] == name[i]:i、j同步后移;
若typed[j] != name[i]:i不动、j后移;
j比i先遍历完,则返回false;否则返回true。
 1 class Solution(object):
 2     def isLongPressedName(self, name, typed):
 3         """
 4         :type name: str
 5         :type typed: str
 6         :rtype: bool
 7         """
 8         # 下标
 9         i = 0
10         j = 0
11         while i < len(name) - 1 and j < len(typed) - 1:
12             if name[i] == typed[j]:
13                 i += 1
14                 j += 1
15             else:
16                 j += 1
17         # i到末尾,j未到,返回true
18         if i == len(name) - 1 and j < len(typed) - 1:
19             return True
20         # i、j都到末尾,且倒数第一个字符相同,返回true
21         elif i == len(name) - 1 and j == len(typed) - 1 and name[-1:] == typed[-1:]:
22             return True
23         # 否则,返回false
24         else:
25             return False
26 
27 if __name__ == '__main__':
28     solution = Solution()
29     print(solution.isLongPressedName("leelee","lleeelee"))
30     print(solution.isLongPressedName("kikcxmvzi", "kiikcxxmmvvzz"))

 

 
posted @ 2020-04-14 14:40  人间烟火地三鲜  阅读(185)  评论(0编辑  收藏  举报