Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
################################################################################
# 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。
################################################################################

*.suo
*.db
*.opendb
*.json
*.sqlite
48 changes: 48 additions & 0 deletions LeafScar/expressive_words/expressive_words.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <vector>

using namespace std;

class Solution {
public:
int expressiveWords(string S, std::vector<string>& words) {
int num = 0;
for (int i = 0; i < words.size(); ++i)
{
if (expressiveWord(S, words[i]))
num++;
}
return num;
}

bool expressiveWord(string s, string t)
{
int s_i = 0;
int t_i = 0;

while (s_i < s.length() && t_i < t.length())
{
if (s[s_i] != t[t_i])
return false;

int s_flag = 0;
while (s[s_i] == t[t_i])
{
s_i++;
s_flag++;
}

int t_flag = 0;
while (s[s_i - 1] == t[t_i])
{
t_flag++;
t_i++;
}

if (t_flag == s_flag || (s_flag >= 3 && t_flag <= s_flag))
continue;

return false;
}
return s_i == s.length() && t_i == t.length();
}
};
Binary file added LeafScar/expressive_words/file.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 93 additions & 0 deletions LeafScar/expressive_words/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# LeetCode 809������зḻ������
### ��Ŀ
&emsp;&emsp;��ʱ�����ǻ��ö������ĸ����ʾ�������У����� "hello" -> "heeellooo", "hi" -> "hiii"�����ǽ���������ͬ����ĸ���飬�������������ĸ������ͬ�����ǽ�һ��ӵ��������������ĸ���鶨��Ϊ����״̬��extended�������һ�������е� "e" ��" o" �Լ��ڶ��������е� "i"�� ���⣬"abbcccaaaa" ���з��� "a" , "bb" , "ccc" , "dddd"������ "ccc" �� "aaaa" ��������״̬��

&emsp;&emsp;����һ���������ַ��� S �������һ�������ܹ�ͨ����һЩ��ĸ�����ŴӶ�ʹ��� S ��ͬ�����ǽ�������ʶ���Ϊ�����ŵģ�stretchy������������ѡ��һ����ĸ�飨�������ĸ c ����Ȼ��������������ͬ����ĸ c ʹ�䳤�ȴﵽ 3 �����ϡ�ע�⣬���Dz��ܽ�һ��ֻ����һ����ĸ����ĸ�飬�� "h"�����ŵ�һ������������ĸ���飬�� "hh"�����е����ű���ʹ����ĸ��������״̬�����ٰ���������ĸ����

&emsp;&emsp;����һ�鵥�ʣ�������п����ŵĵ���������

**ʾ����**
**���룺**
```
S = "heeellooo"
words = ["hello", "hi", "helo"]
```
**�����**
```
1
```
**���ͣ�**
������ͨ������"hello"��"e"��"o"���õ�"heeellooo"��
���Dz���ͨ������"helo"���õ�"heeellooo"��Ϊ"ll"����������״̬��


**˵����**
* 0 <= len(S) <= 100��
* 0 <= len(words) <= 100��
* 0 <= len(words[i]) <= 100��
* S �������� words �еĵ��ʶ�ֻ��Сд��ĸ��ɡ�

### ˼·

**����Ŀ�����⣺**
* ������״̬��ԭ���ʵĵ�����ĸ�������ִ���������󵥴ʶ�Ӧ���������ִ������
* ����״̬��ԭ���ʵĵ�����ĸ�������ִ���������󵥴ʶ�Ӧ��ĸ���Ƚϣ�����󵥴ʶ�Ӧ��ĸ�������ִ�������ԭ���ʵĵ�����ĸ�������ִ������ұ������3��

**ʵ���뷨��**

ԭ����������󵥴�˳��ɨ�裬��¼������ĸ�������ִ������ֱ��Ϊt_flag��s_flag��
![File](file.png)
1. ���˳��ɨ��ʱ������������ĸ��ԭ������ĸ��ͬ��ֱ�ӷ���false��
2. t_flag����s_flag ���� ��s_flag���ڵ���3 �� s_flag��ֵ����t_flag��ֵ���Ž�����һ����ĸ���жϣ�
3. ֱ������һ������ɨ�����������һ�����ʻ�δɨ���꣬��һ����false;

**����ʵ�֣�**
```
bool expressiveWord(string s, string t)
{
int s_i = 0; // ���䵥��ɨ�赽���±�
int t_i = 0; // ԭ����ɨ�赽���±�

while (s_i < s.length() && t_i < t.length())
{
if (s[s_i] != t[t_i])
return false;

int s_flag = 0;
while (s[s_i] == t[t_i]) // ��¼���䵥����ĸ�����ó��ִ���
{
s_i++;
s_flag++;
}

int t_flag = 0;
while (s[s_i - 1] == t[t_i]) // ��¼ԭ������ĸ�����ó��ִ���
{
t_flag++;
t_i++;
}

if (t_flag == s_flag || (s_flag >= 3 && t_flag <= s_flag))
continue;

return false;
}
return s_i == s.length() && t_i == t.length();
}

// ����ȥ���DZ������鵥�ʣ���¼������ϸ������䵥�ʵ�ԭ���ʵ�����
int expressiveWords(string S, std::vector<string>& words) {
int num = 0;
for (int i = 0; i < words.size(); ++i)
{
if (expressiveWord(S, words[i]))
num++;
}
return num;
}
```