๐—–๐—ฆ ๐—”๐—น๐—ด๐—ผ ๐Ÿ’ป ๐ŸŒ ใ€Ž๐—–๐—ผ๐—บ๐—ฝ๐—ฒ๐˜๐—ถ๐˜๐—ถ๐˜ƒ๐—ฒ ๐—ฃ๐—ฟ๐—ผ๐—ด๐—ฟ๐—ฎ๐—บ๐—บ๐—ถ๐—ป๐—ดใ€
9.63K subscribers
5.61K photos
3 videos
95 files
10.6K links
๐ŸšฉMain Group - @SuperExams
๐Ÿ“Job Updates - @FresherEarth

๐Ÿ”ฐAuthentic Coding Solutions(with Outputs)
โš ๏ธDaily Job Updates
โš ๏ธHackathon Updates & Solutions

Buy ads: https://telega.io/c/cs_algo
Download Telegram
vector<int>findSubsequence(vector<int>arr) {
  map<int, vector<int>> ind;
    for (int i = 0; i < arr.size(); ++i) {
        ind[arr[i]].push_back(i);
    }
   
    vector<int> res;
    res.push_back(-1);
   
    for (auto &[_, v] : ind) {
        if (v.size() <= 1) continue;
       
        int cnt = 0;
        for (auto it : v) {
            if (res.back() > it) continue;
            cnt++;
            res.push_back(it);
            if (cnt == v.size() - 1) break;
        }
       
        if (cnt < v.size() - 1) {
            return { -1 };
        }
    }
   
    vector<int> fres;
    for (int i = 1; i < res.size(); ++i) {
        fres.push_back(arr[res[i]]);
    }
   
    return fres;



findSubsequence

Lindkdin all passed
โœ…
import java.util.*;

public class AnagramSubsequenceFinder {
   
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
       
        String S = sc.nextLine();
       
        int N = sc.nextInt();
        sc.nextLine();
        String[] W = new String[N];
        for (int i = 0; i < N; i++) {
            W[i] = sc.nextLine();
        }
       
        int[] freqS = getFrequency(S);
       
        int count = 0;
        for (String w : W) {
            if (isValidAnagramSubsequence(freqS, w)) {
                count++;
            }
        }
       
        System.out.println(count);
    }
   
    private static int[] getFrequency(String S) {
        int[] freq = new int[26];
        for (char c : S.toCharArray()) {
            freq[c - 'a']++;
        }
        return freq;
    }

    private static boolean isValidAnagramSubsequence(int[] freqS, String w) {
        int[] freqW = new int[26];
        for (char c : w.toCharArray()) {
            freqW[c - 'a']++;
        }
       
        for (int i = 0; i < 26; i++) {
            if (freqW[i] > freqS[i]) {
                return false;
            }
        }
       
        return true;
    }
}


// Minimal substring length
def minimumSquads(parents):
    n = len(parents)
    depth = [-1] * n

    def calculate_depth(node):
        if depth[node] != -1:
            return depth[node]
        if parents[node] == -1:
            depth[node] = 0
        else:
            depth[node] = calculate_depth(parents[node]) + 1
        return depth[node]

    max_depth = 0
    for i in range(n):
        max_depth = max(max_depth, calculate_depth(i))

    return max_depth + 1

Minimum squads โœ