Searching 55

QUESTION

Input two strings s and t. The task is to find maximum length of some prefix of the string S which occur in string t as subsequence.

“TESTCASE_1”: “digger\nbiggerdiagram\n###—###SEPERATOR—###—\n3”, “TESTCASE_2”: “geeksforgeeks\nagbcedfeitk\n###—###SEPERATOR—###—\n4”, “TESTCASE_3”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

import java.io.*;
import java.util.*;
// Java program to find maximum length prefix
// of one string occur as subsequence in another
// string.
public class TestClass {     
     
    // Return the maximum length prefix which is
    // subsequence.
    static int maxPrefix(String s, String t)
    {
        int count = 0;
      
        // Iterating string T.
        for (int i = 0; i < t.length(); i++)
        {
            // If end of string S.
            if (count == t.length())
                break;
      
            // If character match, increment 
            // counter.
            if (t.charAt(i) == s.charAt(count))
                count++;
        }
      
        return count;
    }
      
    // Driven Program
    public static void main(String args[])
    {
      Scanner sc=new Scanner(System.in);  
      String S = "";
        String T = "";
      S=sc.next();
      T=sc.next();
      
        System.out.println(maxPrefix(S, T));
    }
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro