PROBLEM LINK:
Author: Prathamesh Sogale
Tester: Prathamesh Sogale
Editorialist: Ram Agrawal
DIFFICULTY:
EASY
PREREQUISITES:
String manupulation
PROBLEM:
It’s a New Year,and Meena has established some goals for the upcoming year.
He decided to build his own YouTube channel and posted some of his films there. Meena now wants to keep
track of all of his YouTube videos so he decided to save all the links to his videos, but youtube links have a
lot of unnecessary strings so he decided to store only the important part of the link that is link id.
You are given String SS.
Help Meena to separate link id from a given string.
Note! You will be given link in two formats
and
here (SCeliffla_k) and (HScledQrT) are Id’s respectively.
QUICK EXPLANATION:
In this Question, we have to check whether the string contains the keyword “view” or “watch” and according to that we have to extract the string Id from the link
EXPLANATION:
In this Question, we have to check whether the string contains the keyword “view” or “watch” .
If the string contains “view” the print Id present after the last “/” or if the string contains “watch” then print id present after “=” and before last “/”.
SOLUTIONS:
Setter's Solution
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i=0 ;i<t ;i++){
String link = sc.next();
int s,e;
if(link.indexOf("v=")==-1){
s=link.lastIndexOf("/")+1;
e=link.length();
}else{
s=link.indexOf("v=")+2;
e=link.lastIndexOf("/");
}
System.out.println(link.substring(s,e));
}
}
}
Tester's Solution
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i=0 ;i<t ;i++){
String link = sc.next();
int s,e;
if(link.indexOf("v=")==-1){
s=link.lastIndexOf("/")+1;
e=link.length();
}else{
s=link.indexOf("v=")+2;
e=link.lastIndexOf("/");
}
System.out.println(link.substring(s,e));
}
}
}
Editorialist's Solution
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
int t;
cin>>t ;
int p = t+1 ;
while (p--)
{
string s ;
getline(cin ,s) ;
int f = s.find('=');
if (f > 0)
{
int pos = s.find('=');
int pos2 = s.find_last_of('/');
cout << s.substr(pos + 1, pos2 - pos - 1) << endl;
}
else
{
int pos = s.find_last_of('/');
cout << s.substr(pos + 1) << endl;
}
}
return 0;
}