【问题标题】:Check whether two strings contain same characters in same order检查两个字符串是否包含相同顺序的相同字符
【发布时间】:2020-04-21 06:07:26
【问题描述】:

''给定两个字符串 s 和 t,编写一个函数来检查 s 是否包含 t 的所有字符(与它们在字符串 t 中的顺序相同)。 返回真或假。 不需要递归。 这是我用java编写的代码的sn-p。 问题在于输入: string1="st3h5irteuyarh!" 和 string2="shrey" 它应该返回 TRUE,但它返回 FALSE。为什么会这样?''

public class Solution {
    public static String getString(char x)  
    { 
       String s = String.valueOf(x); 
       return s; 
    }  

    public static boolean checkSequence(String s1, String s2) 
    {

        String a = getString(s1.charAt(0));  
        String b = getString(s2.charAt(0)); 

        for (int i = 1; i < s1.length(); i++) 
            if (s1.charAt(i) != s1.charAt(i - 1))  
            { 
                a += getString(s1.charAt(i)); 
            } 

        for (int i = 1; i < s2.length(); i++) 
            if (s2.charAt(i) != s2.charAt(i - 1))  
            { 
                b += getString(s2.charAt(i)); 
            } 

        if (!a.equals(b)) 
            return false; 

        return true; 

    }
}

【问题讨论】:

  • 抱歉递归不是必需的。
  • 请批准修改

标签: java string


【解决方案1】:

这是一个解决方案:

public class Solution {
    public static String getString(char x)  
    {

        String s = String.valueOf(x); 
        return s; 
    }

    public static boolean checkSequence(String s1, String s2) 
    {

        String a = getString(s1.charAt(0));  
        String b = getString(s2.charAt(0)); 

        int count = 0;

        for (int i = 0; i < s1.length(); i++)
        {
            if (s1.charAt(i) == s2.charAt(count))  
            { 
                count++;
            } 
            if (count == s2.length())
                return true;
        }

        return false; 
    }
} 
  • String s1 的每个 char 与 String s2 的一个 char 在位置 count 进行比较,
  • 如果他们匹配 count 增加:count++;
  • 如果 count 的长度为 String 2,则所有字符都匹配并返回 true

【讨论】:

  • 哦对不起^_^'现在应该没问题了
【解决方案2】:

我可以在该代码中看到两个问题

1 for (int i = 1; i &lt; s1.length(); i++) 你是从索引1 开始,但字符串索引从0 开始

2 if (s1.charAt(i) != s1.charAt(i - 1)) 在这里,您在其他循环中比较相同字符串 s1 的字符也是这种情况

请先解决这些问题,然后再询问

【讨论】:

    【解决方案3】:

    这可能就是您要搜索的内容

    public class Solution {
      public static boolean checkSequence(String s1, String s2) {
        for(char c : s2.toCharArray()) {
          if(!s1.contains(c+"")) {
            return false;
          }
          int pos = s1.indexOf(c);
          s1 = s1.substring(pos);
        }
        return true;
      }
    }
    

    【讨论】:

      【解决方案4】:

      你解决这个问题的方法可能是这样的:

      1. 找到较小的字符串。
      2. 将指针初始化为较小字符串的起始位置。
      3. 在 for 循环中迭代较大的字符串并继续检查字符是否匹配。
      4. 匹配时增加较小指针的计数器。
      5. 在迭代过程中继续检查较小的指针是否已到达终点。如果是,则返回 true。

      类似这样的:

      public static boolean checkSequence(String s1, String s2)
      {
          String smallerString = s1.length()<=s2.length() ? s1 : s2;
          String largerString = smallerString.equals(s2) ? s1 : s2;
          int smallerStringPointer=0;
          for(int i=0;i<largerString.length();i++){
              if(smallerString.charAt(smallerStringPointer) == largerString.charAt(i)){
                  smallerStringPointer++;
              }
              if(smallerStringPointer == smallerString.length()){
                  return true;
              }
          }
          return false;
      }
      

      【讨论】:

        【解决方案5】:
        public static boolean usingLoops(String str1, String str2) {
            int index = -10;
            int flag = 0;
            for (int i = 0; i < str1.length(); i++) {
                flag = 0;
                for (int j = i; j < str2.length(); j++) {
                    if (str1.charAt(i) == str2.charAt(j)) {
                        if (j < index) {
                            return false;
                        }
                        index = j;
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0)
                    return false;
            }
            return true;
        }
        
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            String str1 = s.nextLine();
            String str2 = s.nextLine();
            // using loop to solve the problem
            System.out.println(usingLoops(str1, str2));
            s.close();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-16
          • 1970-01-01
          • 1970-01-01
          • 2018-05-30
          • 1970-01-01
          相关资源
          最近更新 更多