Join Regular Classroom : Visit ClassroomTech

Strings Based Coding Problems | Firstcharstring | Codewindow.in

Rotate a given String in the specified direction by specified magnitude. After each rotation make a note of the first character of the rotated String, After all rotation are performed the accumulated first character as noted previously will form another string, say FIRSTCHARSTRING. Check If FIRSTCHARSTRING is an Anagram of any substring of the Original string. If yes print “YES” otherwise “NO”.

Input:
The first line contains the original string s. The second line contains a single integer q. The ith of the next q lines contains character d[i] denoting direction and integer r[i] denoting the magnitude.

Constraints:
1 <= Length of original string <= 30
1<= q <= 10

Output:
YES or NO

Input:
carrace
3
L 2
R 2
L 3

Output:
NO

Explanation:
After applying all the rotations the FIRSTCHARSTRING string will be “rcr” which is not anagram of any sub string of original string “carrace”.

Solution: In C

#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;

int isSubstring(string s1, string s2)
{
    int M = s1.length();
    int N = s2.length();
    for (int i = 0; i <= N - M; i++) {
        int j;
        for (j = 0; j < M; j++)
            if (s2[i + j] != s1[j])
                break;
        if (j == M)
            return i;
    }
    return -1;
}

int main() {
    string s;
    cin >> s;
    int sz=s.length();
    string fc;
    int q;
    scanf("%d",&q);
    int i=0;
    while(q--){
        char c;
        int m;
        scanf("%c %d",&c,&m);
        if(c=='L'){
            i=(i+m)%sz;
        }
        if(c=='R'){
            i=(i-m)%sz;
        }
        fc.push_back(s[i]);
    }
    bool f=false;
    for(int i=0;i<sz;i++){
        for(int j=i;j<sz;j++){
            if(isSubstring(fc,s.substr(i,j-i+1))>=0) {
                f=true;
            }
        }
    }
    if(f) printf("Yes");
    else printf("No");
}

Solution: In Java

import java.util.Arrays;
import java.util.Scanner;
import java.io.*;

public class Main {
    static String sample;
    static String s1="";
    
    public static String leftrotate(String str, int d)
    {
        sample = str.substring(d) + str.substring(0, d);
        char fl = sample.charAt(0);
        s1=s1+String.valueOf(fl);
        return sample;
    }
    
    public static void rightrotate(String str, int d)
    {
        sample=leftrotate(str, str.length() - d);
    }
    
    public static boolean Anagram(char[] str1, char[] str2)
    {
        int len1 = str1.length;
        int len2 = str2.length;
        if (len1 != len2)
            return false;
        Arrays.sort(str1);
        Arrays.sort(str2);
        for (int i = 0; i < len1; i++)
            if (str1[i] != str2[i])
                return false;
        return true;
    }
    
    public static void main(String args[]) throws Exception
    {
        String[] dir;
        int[] mag;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        sample = br.readLine();
        int q=Integer.parseInt(br.readLine());
        dir = new String[q];
        mag = new int[q];
        for(int x=0;x<q;x++)
        {
            String[] inp = br.readLine().split(" ");
            dir[x]= inp[0];
            mag[x]= Integer.parseInt(inp[1]);
        }
        for(int i=0;i<q;i++)
        {
            if(dir[i].equals("L"))
            {
                int temp=mag[i];
                leftrotate(sample, temp);
            }
            if(dir[i].equals("R"))
            {
                int temp=mag[i];
                rightrotate(sample, temp);
            }
        }
        char[] ans1 = sample. toCharArray();
        char[] s2 = s1. toCharArray();
        if (Anagram(ans1, s2))
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}

Solution: In Python 3

def ana(a,b):
    if len(a)<len(b):
        return False
    cb=CharCounts(b)
    for i in range(0,len(a)-len(b)+1):
        window=a[i:i+len(b)]
        cw=CharCounts(window)

def CharCounts(s):
    counts=dict()
    for char in s:
        if char not in counts:
            counts[char]=1
        else:
            counts[char]+=1
    return counts


word=input().strip()
first=[]
i=0
q=int(input())
for _ in range(q):
    d,s=input().strip().split()
    s=int(s)
    if d=="L":
        i=(i+s)%len(word)
    else:
        i=(i-s)%len(word)
    first.append(word[i])
if ana(word,first):
    print("Yes")
else:
    print("No")

Also Checkout

Recent Posts
Categories
Pages