Manna's First Name

Given a string containing any ASCII character. Find out the number of times "SUVO" and "SUVOJIT"
appears in it.

Example

Input: 
3
SUVOJITSU
651SUVOMN
$$$$$SUVOSUVOJIT$$$$$
Output:
SUVO = 0, SUVOJIT = 1
SUVO = 1, SUVOJIT = 0
SUVO = 1, SUVOJIT = 1

Approach:

Java

import java.util.Scanner;
public class MannasFirstName {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        in.nextLine();
        for (int i = 0; i < N; i++) {
            String str = in.nextLine();
            mannFirstName(str);
        }
        in.close();
    }

    private static void mannFirstName(String s) {
        int n = s.length();
        int cnt1 = 0, cnt2 = 0;
        String str = "SUVO", str1 = "SUVOJIT";
        for (int i = 0; i <= n - 4; i++) {
            int flag = 0;
            if (n - i >= 7) {
                for (int j = 0; j < 7; j++) {
                    if (s.charAt(i + j) != str1.charAt(j)) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 0) {
                    cnt2++;

                } else if (flag == 1) {
                    int flag1 = 0;
                    for (int j = 0; j < 4; j++) {
                        if (s.charAt(i + j) != str.charAt(j)) {
                            flag1 = 1;
                            break;
                        }
                    }
                    if (flag1 == 0) {
                        cnt1++;

                    }
                }

            } else {
                flag = 0;
                for (int j = 0; j < 4; j++)
                    if (s.charAt(i + j) != str.charAt(j)) {
                        flag = 1;
                        break;
                    }
                if (flag == 0) {
                    cnt1++;

                }
            }

        }
        System.out.println("SUVO = " + cnt1 + ", SUVOJIT = " + cnt2);

    }
}

C++

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
     {
         string s;
         cin>>s;
         int n=s.size();
         int cnt1=0,cnt2=0;
         string str="SUVO",str1="SUVOJIT";
         for(int i=0;i<=n-4;i++)
         {
                 int flag=0;
                 if(n-i>=7)
                 {
                 for(int j=0;j<7;j++)
                 {
                         if(s[i+j]!=str1[j])
                         {
                                 flag=1;
                                 break;
                             }
                     }
                    if(flag==0)
                         {
                             cnt2++;
                            
                         }
                    else if(flag==1)
                     {
                         int flag=0;
                         for(int j=0;j<4;j++)
                         {
                                 if(s[i+j]!=str[j])
                                 {
                                         flag=1;
                                         break;
                                     }
                             }
                            if(flag==0)
                     {
                                     cnt1++;
                              
                                 }
                     }
                
                 }
                 else
                 {
                     int flag=0;
                     for(int j=0;j<4;j++)
                     if(s[i+j]!=str[j])
                         {
                                 flag=1;
                                 break;
                             }
                     if(flag==0)
                     {
                             cnt1++;
         
                         }
                 }
                    
             }
            cout<<"SUVO = "<<cnt1<<", "<<"SUVOJIT = "<<cnt2<<"\n";
     }
    return 0;
}



No comments:

Post a Comment