检查字符串 S 是否可以通过用 count X 替换一些 X字符来压缩为 T
给定两个字符串, S和T ,其中S是普通字符串, T是压缩字符串,任务是确定压缩字符串T是否可以通过压缩字符串S来实现。
注意:压缩机制可以任意删除 X (X >= 0) 个字符,并用删除的字符数 (X) 替换它们。
例子:
Input: S = “HELLOWORLD” T=”H2L4L1″
Output: True
Explanation:
Replace index 1 to 2 in “HELLOWORLD” with count 2 -> “H2LOWORLD”
Replace index 4 to 7 in “HELLOWORLD” with count 4 -> “H2L4LD”
Replace index 9 in “HELLOWORLD” with count 1 -> “H2L4L1“
The resultant string is same as T. Hence String S can be compressed to T.
Input: S = “DFS” T=”D1D”
Output: False
Explanation: We can clearly see that T is not a valid compressed string for S as compressed string T is ending with “D” which is not similar with the former string S.
做法:思路就是简单的遍历字符串,匹配字符如下:
- 比较 S 和 T 中相应索引处的字符,
- 同样跳过 T 中两个字母之间的索引计数。
插图:
Consider S = “HELLOWORLD” T=”H2L4L1″
In compressed string, 2 is the integer between ‘H’ and ‘L’,so string S must has any two characters between ‘H’ and ‘L’, and it has (HELLOWORLD).
Then next integer is 4 between ‘L’ and ‘L’, then check whether String S has 4 characters or not, (HELLOWORLD)
And last integer is 1 after character ‘L’ and String also has 1 character(i.e ‘D’) after ‘L’. (HELLOWORLD)
So, this Compressed String is valid.
S = “HELLOWORLD” T=”H2L4L1“
请按照以下步骤解决问题:
- 开始同时遍历 S 和 T字符串直到它的长度。
- 检查 T[i] 是否为数字,如果不是数字则比较 S[j] 和 T[i] 如果它们不相等则直接返回 0 否则继续并将 j 增加 1 。
- 如果 T[i] 是一个数字,则将 j 增加到该数字,直到我们得到 T 字符串中的数字。
- 将 i 增加 1 直到 T 的长度。
- 如果 j 不等于 S 的长度,则返回 0。
- 如果满足所有条件,则最后返回 1。
下面是上述方法的实现:
C++
// C++ code for the above approach:
#include
using namespace std;
// Return 1 if char c is number
boolean isnum(char c) { return (c >= 48 && c <= 57); }
// Function to check
// If string is compressed or not
int checkCompressed(string S, string T)
{
int i = 0, j = 0;
// Iterate till S.length()
// And T.length
while (j < S.length() && i < T.length()) {
if (isnum(T[i]) == false) {
// If its not equal
// Then return 0
if (S[j] != T[i]) {
return 0;
}
j++;
}
else {
int ans = T[i] - 48;
// Iterate till we get number
// In T string
while (isnum(T[++i])) {
ans *= 10;
ans += T[i] - 48;
}
j += ans;
i--;
}
i++;
}
// It j not equal to S string length
// Then return 0
if (j != S.length()) {
return 0;
}
return 1;
}
// Driver code
int main()
{
string S = "HelloWorld";
string T = "H2l4l1";
cout << checkCompressed(S, T) << endl;
return 0;
}
Java
// Java code for the above approach:
import java.util.*;
class GFG{
// Return 1 if char c is number
static boolean isnum(char c) { return (c >= 48 && c <= 57); }
// Function to check
// If String is compressed or not
static int checkCompressed(char[] S, char[] T)
{
int i = 0, j = 0;
// Iterate till S.length()
// And T.length
while (j < S.length && i < T.length) {
if (isnum(T[i]) == false) {
// If its not equal
// Then return 0
if (S[j] != T[i]) {
return 0;
}
j++;
}
else {
int ans = T[i] - 48;
// Iterate till we get number
// In T String
while (i
Python3
# python3 code for the above approach:
# Return 1 if char c is number
def isnum(c):
return ord(c) >= 48 and ord(c) <= 57
# Function to check
# If string is compressed or not
def checkCompressed(S, T):
i, j = 0, 0
# Iterate till S.length()
# And T.length
while (j < len(S) and i < len(T)):
if (isnum(T[i]) == False):
# If its not equal
# Then return 0
if (S[j] != T[i]):
return 0
j += 1
else:
ans = ord(T[i]) - 48
# Iterate till we get number
# In T string
i += 1
while (i < len(T) and isnum(T[i])):
ans *= 10
ans += T[i] - 48
i += 1
j += ans
i -= 1
i += 1
# It j not equal to S string length
# Then return 0
if (j != len(S)):
return 0
return 1
# Driver code
if __name__ == "__main__":
S = "HelloWorld"
T = "H2l4l1"
print(checkCompressed(S, T))
# This code is contributed by rakeshsahni
C#
// C# code for the above approach:
using System;
public class GFG{
// Return 1 if char c is number
static boolean isnum(char c) { return (c >= 48 && c <= 57); }
// Function to check
// If String is compressed or not
static int checkCompressed(char[] S, char[] T)
{
int i = 0, j = 0;
// Iterate till S.length()
// And T.length
while (j < S.Length && i < T.Length) {
if (isnum(T[i]) == false) {
// If its not equal
// Then return 0
if (S[j] != T[i]) {
return 0;
}
j++;
}
else {
int ans = T[i] - 48;
// Iterate till we get number
// In T String
while (i
Javascript
1
时间复杂度: O(max (|S|, |T|) )
辅助空间: O(1)