The_Eternal_Wisp
The_Eternal_Wisp

Reputation: 5

Check whether two strings contain same characters in same order

''Given two string s and t, write a function to check if s contains all characters of t (in the same order as they are in string t). Return true or false. recursion not necessary. here is the snippet of code that I am writing in java. problem is for input: string1="st3h5irteuyarh!" and string2="shrey" it should return TRUE but it is returning FALSE. Why is that?''

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; 

    }
}

Upvotes: 0

Views: 2671

Answers (5)

Amit Rana
Amit Rana

Reputation: 19

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();
}

Upvotes: 0

axolotl
axolotl

Reputation: 103

This is a solution:

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; 
    }
} 
  • Each char of String s1 is compared with a char of String s2 at position count,
  • if they match count increases: count++;
  • If count has the length of String 2 all chars matched and true is returned.

Upvotes: 1

Abhishek
Abhishek

Reputation: 1618

Your approach to solve this problem can be something like this :

  1. Find the smaller string.
  2. Initialise the pointer to starting position of smaller string.
  3. Iterate over the larger string in for loop and keep checking if character is matching.
  4. On match increase the counter of smaller pointer.
  5. while iterating keep checking if smaller pointer has reached to end or not. If yes then return true.

Something like this :

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;
}

Upvotes: 0

Amir Schnell
Amir Schnell

Reputation: 651

this could be what you are searching for

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;
  }
}

Upvotes: 0

Abhinav Chauhan
Abhinav Chauhan

Reputation: 1384

there are two problems i can see in that code

1 for (int i = 1; i < s1.length(); i++) you are starting from index 1 but string indexes starts from 0

2 if (s1.charAt(i) != s1.charAt(i - 1)) here you are comparing characters of same strings s1 in other loop also this is the case

please fix these first, then ask again

Upvotes: 0

Related Questions