Skip to content Navigation Menu Toggle navigation Sign in Appearance settings PlatformAI CODE CREATIONGitHub CopilotWrite better code with AIGitHub Copilot appDirect agents from issue to mergeMCP RegistryNewIntegrate external toolsDEVELOPER WORKFLOWSActionsAutomate any workflowCodespacesInstant dev environmentsIssuesPlan and track workCode ReviewManage code changesAPPLICATION SECURITYGitHub Advanced SecurityFind and fix vulnerabilitiesCode securitySecure your code as you buildSecret protectionStop leaks before they startEXPLOREWhy GitHubDocumentationBlogChangelogMarketplaceView all featuresSolutionsBY COMPANY SIZEEnterprisesSmall and medium teamsStartupsNonprofitsBY USE CASEApp ModernizationDevSecOpsDevOpsCI/CDView all use casesBY INDUSTRYHealthcareFinancial servicesManufacturingGovernmentView all industriesView all solutionsResourcesEXPLORE BY TOPICAISoftware DevelopmentDevOpsSecurityView all topicsEXPLORE BY TYPECustomer storiesEvents & webinarsEbooks & reportsBusiness insightsGitHub SkillsSUPPORT & SERVICESDocumentationCustomer supportCommunity forumTrust centerPartnersView all resourcesOpen SourceCOMMUNITYGitHub SponsorsFund open source developersPROGRAMSSecurity LabMaintainer CommunityAcceleratorGitHub StarsArchive ProgramREPOSITORIESTopicsTrendingCollectionsEnterpriseENTERPRISE SOLUTIONSEnterprise platformAI-powered developer platformAVAILABLE ADD-ONSGitHub Advanced SecurityEnterprise-grade security featuresCopilot for BusinessEnterprise-grade AI featuresPremium SupportEnterprise-grade 24/7 supportPricing Search or jump to... Search code, repositories, users, issues, pull requests... Search Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. Include my email address so I can be contacted Saved searches Use saved searches to filter your results more quickly Name Query To see all available qualifiers, see our documentation. Sign in Sign up Appearance settings Resetting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} rajeevranjancom / Leetcode_Java Public Notifications You must be signed in to change notification settings Fork 0 Star 0 Code Issues 0 Pull requests 0 Actions Projects Security and quality 0 Insights Additional navigation options Code Issues Pull requests Actions Projects Security and quality Insights FilesExpand file tree masterBreadcrumbsLeetcode_Java/392_IsSubsequence392.javaCopy pathBlameMore file actionsBlameMore file actions Latest commit HistoryHistoryHistory80 lines (75 loc) · 2.33 KB masterBreadcrumbsLeetcode_Java/392_IsSubsequence392.javaCopy pathTopFile metadata and controlsCodeBlame80 lines (75 loc) · 2.33 KBRawCopy raw fileDownload raw fileOpen symbols panelEdit and raw actions1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980/** * Given a string s and a string t, check if s is subsequence of t. * * You may assume that there is only lower case English letters in both s and t. * t is potentially a very long (length ~= 500,000) string, and s is a short * string (<=100). * * A subsequence of a string is a new string which is formed from the original * string by deleting some (can be none) of the characters without disturbing * the relative positions of the remaining characters. (ie, "ace" is a * subsequence of "abcde" while "aec" is not). * * Example 1: * s = "abc", t = "ahbgdc" * Return true. * * Example 2: * s = "axc", t = "ahbgdc" * Return false. * * Follow up: * If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you * want to check one by one to see if T has its subsequence. In this scenario, * how would you change your code? */ public class IsSubsequence392 { public boolean isSubsequence(String s, String t) { if (s == null && t == null) return true; if (s == null || t == null) return true; int lenS = s.length(); int lenT = t.length(); char[] charS = s.toCharArray(); char[] charT = t.toCharArray(); int i = 0; int j = 0; while (i < lenS && j < lenT) { while (j < lenT && charT[j] != charS[i]) { j++; } if (j == lenT) break; j++; i++; } return i == lenS; } /** * https://leetcode.com/problems/is-subsequence/discuss/87297/Java.-Only-2ms.-Much-faster-than-normal-2-pointers. */ public boolean isSubsequence2(String s, String t) { if(t.length() < s.length()) return false; int prev = 0; for(int i = 0; i < s.length();i++) { char tempChar = s.charAt(i); prev = t.indexOf(tempChar,prev); if(prev == -1) return false; prev++; } return true; } public boolean isSubsequence3(String s, String t) { int i = 0; int j = 0; char[] chars = s.toCharArray(); char[] chart = t.toCharArray(); while (i < chars.length && j < chart.length) { if (chars[i] == chart[j]) { i++; j++; } else { j++; } } return i == chars.length; } } You can’t perform that action at this time.