A love Guru determines "whether a guy's proposal is going to be accepted by his crush or not" just by doing some mysterious calculation on their names, but it takes too much time for him. So, he hired you as a programmer and now your task is to write a program that helps the Guru.
He reveals his mystery with you and that is:
If the guy's name is a subsequence of his crush's name, then she is going to accept him, otherwise, she will reject him.
Example:
Input: s1 = "rahul"; s2 = "allgirlsallhunontheplanet";
Output: Love you too
Approach
Java
public class WillsheAccept {public static void main(String args[]) throws Exception {String s1 = "rahul";String s2 = "allgirlsallhunontheplanet";if (checkSubsequence(s1, s2)) {System.out.println("Love you too");} else {System.out.println("We are only friends");}}static boolean checkSubsequence(String s1, String s2) {int currentIndex = 0;for (int index = 0; index < s1.length(); index++) {char currentChar = s1.charAt(index);currentIndex = s2.indexOf(currentChar, currentIndex);if (currentIndex == -1) {return false;}}return true;}}
C++
#include <bits/stdc++.h>using namespace std;bool checkSubsequence(string s1, string s2){int currentIndex = 0;for (int index = 0; index < s1.length(); index++){char currentChar = s1[index];currentIndex = s2.find(currentChar, currentIndex);if (currentIndex == -1){return false;}}return true;}int main(){string s1 = "rahul";string s2 = "allgirlsallhunontheplanet";if (checkSubsequence(s1, s2))cout << "Love you too\n";elsecout << "We are only friends\n";return 0;}
No comments:
Post a Comment