检查 String2 是否是带有 '*' 和 '\' 的 string1 的子串
Check whether String2 is substring of string1 with '*' and '\'
给定两个字符串 s1 和 s2,我们必须检查 s2 是否是 s1 的子字符串。
如果是真的打印“真”否则打印“假”
但是 s1 和 s2 可以包含像 '*' 和 '\' 这样的字符。
在 s2 中,'*' 表示两个字母之间的零个或多个字符,'\' 表示 '*' 的转义序列。
在s1中,'*'和'\'只是其他要检查的字符。
示例输入和输出:
Input: abcd , a*c Output: true
Input : spoon , sp*n Output : true
Input : regex , re*g Output : true
Input : search , *c Output : true
Input : zoho , *o*o Output : true
Input : zoho , *ogo Output : false
Input : test , pest Output : false
Input : st*r , t\*r Output : true
Input : star , t\*r Output false
Input : tree , tr\ Output false
Input : tr\e , tr\ Output true
我知道这个问题可以通过使用 regex 轻松解决,但是 我需要一个合乎逻辑的方法 来解决这个问题。有人可以尽快帮助我吗?
以下是我尝试过但无法完全解决的代码。
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String s1 = sc.next();
String s2 = sc.next();
boolean flag = true;
int i=0,j=0;
for(;s2.charAt(j)<'a' || s2.charAt(j)>'z';j++);
for(i=0;i<s1.length();i++)
{
char ch = s1.charAt(i);
if(ch==s2.charAt(j))
{
i++;j++;
break;
}
}
for(;j<s2.length();)
{
System.out.println(i+" "+j);
while(s2.charAt(j)=='*' && j<s2.length()-1 && s2.charAt(j+1)!=s1.charAt(i) && i<s1.length())
{
i++;
}
j++;
if(i==s1.length())
{
flag = false;
break;
}
if(s1.charAt(i)!=s2.charAt(j) && s2.charAt(j)<97 && s2.charAt(j)>122)
{
flag = false;
break;
}
}
System.out.print(flag);
}
我不确定 * 和 / 是什么意思,但是,要知道一个字符串是否是另一个字符串的子字符串的一种解决方案是使用“包含”方法,该方法从 String class:
public static void main(String[] args) {
String str1 = "abcdefghi";
String str2 = "abcd";
if (str1.contains(str2)) {
System.out.println("Exist the substring.");
} else {
System.out.println("Not exist.");
}
}
我认为您还需要将 * 或 / 替换为有效值
您可以剥离 * 的字符串,并在遇到 * 字符时将其断开,并检查子字符串是否在实际字符串中。类似于:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.next();
String s2 = sc.next();
int begIndex = 0;
int prevIndex = 0;
boolean flag = true;
s2 = stripLeadingAndTrailingCharacter(s2, '*');
for(int i = 0; i < s2.length(); ++i) {
if ((( i == 0 || s2.charAt(i - 1) != '\') && s2.charAt(i) == '*')
|| (i == s2.length() - 1)) {
int endIndex = i == s2.length() - 1 ? i + 1 : i;
String str = s2.substring(begIndex, endIndex).replace("\*", "*");
if (s1.contains(str) && prevIndex <= s1.indexOf(str)) {
prevIndex = s1.indexOf(str);
} else {
flag = false;
break;
}
begIndex = i + 1;
}
}
System.out.println(flag);
}
public static String stripLeadingAndTrailingCharacter(String s, char ch) {
int index;
for (index = 0; index < s.length(); index++) {
if (s.charAt(index) != ch) {
break;
}
}
s = s.substring(index);
for (index = s.length() - 1; index >= 0; index--) {
if (s.charAt(index) != ch) {
break;
}
}
s = s.substring(0, index + 1);
return s;
}
给定两个字符串 s1 和 s2,我们必须检查 s2 是否是 s1 的子字符串。 如果是真的打印“真”否则打印“假” 但是 s1 和 s2 可以包含像 '*' 和 '\' 这样的字符。 在 s2 中,'*' 表示两个字母之间的零个或多个字符,'\' 表示 '*' 的转义序列。 在s1中,'*'和'\'只是其他要检查的字符。
示例输入和输出:
Input: abcd , a*c Output: true
Input : spoon , sp*n Output : true
Input : regex , re*g Output : true
Input : search , *c Output : true
Input : zoho , *o*o Output : true
Input : zoho , *ogo Output : false
Input : test , pest Output : false
Input : st*r , t\*r Output : true
Input : star , t\*r Output false
Input : tree , tr\ Output false
Input : tr\e , tr\ Output true
我知道这个问题可以通过使用 regex 轻松解决,但是 我需要一个合乎逻辑的方法 来解决这个问题。有人可以尽快帮助我吗? 以下是我尝试过但无法完全解决的代码。
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String s1 = sc.next();
String s2 = sc.next();
boolean flag = true;
int i=0,j=0;
for(;s2.charAt(j)<'a' || s2.charAt(j)>'z';j++);
for(i=0;i<s1.length();i++)
{
char ch = s1.charAt(i);
if(ch==s2.charAt(j))
{
i++;j++;
break;
}
}
for(;j<s2.length();)
{
System.out.println(i+" "+j);
while(s2.charAt(j)=='*' && j<s2.length()-1 && s2.charAt(j+1)!=s1.charAt(i) && i<s1.length())
{
i++;
}
j++;
if(i==s1.length())
{
flag = false;
break;
}
if(s1.charAt(i)!=s2.charAt(j) && s2.charAt(j)<97 && s2.charAt(j)>122)
{
flag = false;
break;
}
}
System.out.print(flag);
}
我不确定 * 和 / 是什么意思,但是,要知道一个字符串是否是另一个字符串的子字符串的一种解决方案是使用“包含”方法,该方法从 String class:
public static void main(String[] args) {
String str1 = "abcdefghi";
String str2 = "abcd";
if (str1.contains(str2)) {
System.out.println("Exist the substring.");
} else {
System.out.println("Not exist.");
}
}
我认为您还需要将 * 或 / 替换为有效值
您可以剥离 * 的字符串,并在遇到 * 字符时将其断开,并检查子字符串是否在实际字符串中。类似于:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.next();
String s2 = sc.next();
int begIndex = 0;
int prevIndex = 0;
boolean flag = true;
s2 = stripLeadingAndTrailingCharacter(s2, '*');
for(int i = 0; i < s2.length(); ++i) {
if ((( i == 0 || s2.charAt(i - 1) != '\') && s2.charAt(i) == '*')
|| (i == s2.length() - 1)) {
int endIndex = i == s2.length() - 1 ? i + 1 : i;
String str = s2.substring(begIndex, endIndex).replace("\*", "*");
if (s1.contains(str) && prevIndex <= s1.indexOf(str)) {
prevIndex = s1.indexOf(str);
} else {
flag = false;
break;
}
begIndex = i + 1;
}
}
System.out.println(flag);
}
public static String stripLeadingAndTrailingCharacter(String s, char ch) {
int index;
for (index = 0; index < s.length(); index++) {
if (s.charAt(index) != ch) {
break;
}
}
s = s.substring(index);
for (index = s.length() - 1; index >= 0; index--) {
if (s.charAt(index) != ch) {
break;
}
}
s = s.substring(0, index + 1);
return s;
}