检查Java中检查字符串是否为另一个字符串的子序列
时间:2020-01-09 10:35:23 来源:igfitidea点击:
编写一个Java程序来检查给定的String是否是另一个Java采访中经常问到的另一个字符串的子序列。
如果我们有两个字符串str1和str2,则如果在str2中找到了str1中的所有字符,则str1是str2的子序列,因此可能无法连续找到字符,但字符应该是有序的。
例如,如果str1 =" code"且str2 =" cportde",则str1是str2的子序列,因为我们可以通过删除str2中的某些字符,以相同的顺序获得str2中所有str1的字符。
如果str1 =" abc"且str2 =" bsdafgc",则str1不是str2的子序列,因为str1的所有字符都存在于str2中,但顺序不同。
Java程序检查字符串是否为另一个字符串的子序列
要检查字符串str1是否是str2的子序列,请从str1的第一个字符开始,并迭代字符串str2以检查是否找到了该字符。
如果是,则移至str1的下一个字符,并在str2中进行检查。
如果否,则在str2中检查str1的相同字符。
有递归和迭代Java程序,用于检查String是否为另一个字符串的子序列。在本文中,我们将介绍两种方式。
迭代Java程序,用于检查String是否为另一个字符串的子序列
public class SubSequenceChecker { public static void main(String[] args) { String str1 = "code"; String str2 = "cportde"; boolean subSeqFlag = isSubSequenceFound(str1, str2); displayResult(str1, str2, subSeqFlag); str1 = "abc"; str2 = "bsdafgc"; subSeqFlag = isSubSequenceFound(str1, str2); displayResult(str1, str2, subSeqFlag); str1 = "theitroad"; str2 = "theitroadcom"; subSeqFlag = isSubSequenceFound(str1, str2); displayResult(str1, str2, subSeqFlag); } private static boolean isSubSequenceFound(String str1, String str2){ int j = 0; for(int i = 0; i < str2.length(); i++){ // If char found move to next char if(str1.charAt(j) == str2.charAt(i)){ ++j; } // Equal means all the characters of str1 are // found in str2 in order if(j == str1.length()){ return true; } } return false; } private static void displayResult(String str1, String str2, boolean flag) { if(flag) System.out.println(str1 + " is a subsequence of " + str2); else System.out.println(str1 + " is not a subsequence of " + str2); } }
输出量
code is a subsequence of cportde abc is not a subsequence of bsdafgc theitroad is a subsequence of
递归Java程序,检查String是否为另一个字符串的子序列
public class SubSequenceChecker { public static void main(String[] args) { String str1 = "code"; String str2 = "cportde"; boolean subSeqFlag = isSubSequenceFound(str1, str2, 0, 0); displayResult(str1, str2, subSeqFlag); str1 = "abc"; str2 = "bsdafgc"; subSeqFlag = isSubSequenceFound(str1, str2, 0, 0); displayResult(str1, str2, subSeqFlag); str1 = "theitroad"; str2 = "theitroadcom"; subSeqFlag = isSubSequenceFound(str1, str2, 0, 0); displayResult(str1, str2, subSeqFlag); } /** * Checking if str1 is a subsequence of str2 */ private static boolean isSubSequenceFound(String str1, String str2, int str1Index, int str2Index){ // exit condition-1 // All the chars in str1 are found if(str1.length() == str1Index) { return true; } // exit condition-2 // Str2 is completely iterated without finding all the // chars in str1 if(str2.length() == str2Index) { return false; } // if char is found move both strings by one char // otherwise only move str2 by one char if(str1.charAt(str1Index) == str2.charAt(str2Index)){ return isSubSequenceFound(str1, str2, ++str1Index, ++str2Index); }else{ return isSubSequenceFound(str1, str2, str1Index, ++str2Index); } } private static void displayResult(String str1, String str2, boolean flag) { if(flag) System.out.println(str1 + " is a subsequence of " + str2); else System.out.println(str1 + " is not a subsequence of " + str2); } }
输出量
code is a subsequence of cportde abc is not a subsequence of bsdafgc theitroad is a subsequence of theitroadcom