Permutation in String || NEETCODE 150

Поделиться
HTML-код
  • Опубликовано: 5 ноя 2024

Комментарии • 7

  • @soumyopandey7974
    @soumyopandey7974 9 месяцев назад

    Good Approach...didi...keep it up.😊.

  • @ankitraj2283
    @ankitraj2283 9 месяцев назад +1

    Day 18 Done ✅✅
    i have doubt after looking this Question directly Sidling Window Concept come in mind not Brute Force Approach
    Should i first go for brute Force

  • @prathamgoel3386
    @prathamgoel3386 9 месяцев назад

    class Solution {
    public boolean checkInclusion(String s1, String s2) {
    if (s1.length() > s2.length()) {
    return false;
    }
    int[] c1 = new int[26];
    int[] c2 = new int[26];
    for (int i = 0; i < s1.length(); i++) {
    c1[s1.charAt(i) - 'a']++;
    c2[s2.charAt(i) - 'a']++;
    }
    for (int i = s1.length(); i < s2.length(); i++) {
    if (isEqual(c1, c2)) {
    return true;
    }
    c2[s2.charAt(i) - 'a']++;
    c2[s2.charAt(i - s1.length()) - 'a']--;
    }
    if (isEqual(c1, c2)) {
    return true;
    }
    return false;
    }
    private boolean isEqual(int[] c1, int[] c2) {
    for (int i = 0; i < c1.length; i++) {
    if (c1[i] != c2[i]) {
    return false;
    }
    }
    return true;
    }
    }

  • @ankitraj2283
    @ankitraj2283 9 месяцев назад +1

    My Approach
    class Solution {
    public boolean checkInclusion(String s1, String s2) {
    HashMap map = new HashMap();
    int i = 0;
    int j = 0;
    int k = s1.length();
    int n = s2.length();
    while (j < n) {
    map.put(s2.charAt(j), map.getOrDefault(s2.charAt(j), 0) + 1);
    while (j - i + 1 == k) {
    if (check(map, s1)) {
    return true;
    }
    map.put(s2.charAt(i), map.getOrDefault(s2.charAt(i), 0) - 1);
    if (map.get(s2.charAt(i)) == 0) {
    map.remove(s2.charAt(i));
    }
    i++;
    }
    j++;
    }
    return false;
    }
    static boolean check(HashMap map, String s1) {
    HashMap targetMap = new HashMap();
    for (char c : s1.toCharArray()) {
    targetMap.put(c, targetMap.getOrDefault(c, 0) + 1);
    }
    return map.equals(targetMap);
    }
    }

  • @mayappapujari9259
    @mayappapujari9259 9 месяцев назад

    Done ✌

  • @prathamgoel3386
    @prathamgoel3386 9 месяцев назад

    My Brute force approach
    class Solution {
    private void generatePermutations(String prefix, String remain, Set pers) {
    if (remain.length() == 0) {
    pers.add(prefix);
    } else {
    for (int i = 0; i < remain.length(); i++) {
    char curr = remain.charAt(i);
    String newPrefix = prefix + curr;
    String newRemain = remain.substring(0, i) + remain.substring(i + 1);
    generatePermutations(newPrefix, newRemain, pers); // Corrected variable name
    }
    }
    }
    public boolean checkInclusion(String s1, String s2) {
    if (s1.length() == 0) {
    return false;
    }
    Set permutations = new HashSet(); // Corrected variable name
    generatePermutations("", s1, permutations);
    for (String permu : permutations) {
    if (s2.contains(permu)) {
    return true;
    }
    }
    return false;
    }
    }