Skip to main content
edited tags
Link
pacmaninbw
  • 26.1k
  • 13
  • 47
  • 114

I am working on a problem on LeetCode. The link to it is https://leetcode.com/problems/permutation-in-string/description/?envType=problem-list-v2&envId=stringon LeetCode.

Considering only lowercase characters from 'a' to 'z', I have written some Java code to check if a permutation of a given pattern is a substring of a given string. I have used the sliding window technique to determine the output. Could someone please let me know if my approach is optimal in terms of time complexity and space complexity? Any help with determining the time complexity would be appreciated as well.

public static boolean permSubstring(String txt, String pat)
  {
    char[] patTempArray = pat.toCharArray();
    Arrays.sort(patTempArray);
    
    String patResult = new String(patTempArray);
    
    for(int j = 0; j <= (txt.length() - pat.length()); j++)
    {
            
        String check = txt.substring(j, j + (pat.length()));
        char[] tempSub = check.toCharArray();
        Arrays.sort(tempSub);
        String checkResult = new String(tempSub);
        
        if(checkResult.equals(patResult))
        {
            return true;
        }
    }
    
    return false;
  }

I am working on a problem on LeetCode. The link to it is https://leetcode.com/problems/permutation-in-string/description/?envType=problem-list-v2&envId=string

Considering only lowercase characters from 'a' to 'z', I have written some Java code to check if a permutation of a given pattern is a substring of a given string. I have used the sliding window technique to determine the output. Could someone please let me know if my approach is optimal in terms of time complexity and space complexity? Any help with determining the time complexity would be appreciated as well.

public static boolean permSubstring(String txt, String pat)
  {
    char[] patTempArray = pat.toCharArray();
    Arrays.sort(patTempArray);
    
    String patResult = new String(patTempArray);
    
    for(int j = 0; j <= (txt.length() - pat.length()); j++)
    {
            
        String check = txt.substring(j, j + (pat.length()));
        char[] tempSub = check.toCharArray();
        Arrays.sort(tempSub);
        String checkResult = new String(tempSub);
        
        if(checkResult.equals(patResult))
        {
            return true;
        }
    }
    
    return false;
  }

I am working on a problem on LeetCode.

Considering only lowercase characters from 'a' to 'z', I have written some Java code to check if a permutation of a given pattern is a substring of a given string. I have used the sliding window technique to determine the output. Could someone please let me know if my approach is optimal in terms of time complexity and space complexity? Any help with determining the time complexity would be appreciated as well.

public static boolean permSubstring(String txt, String pat)
  {
    char[] patTempArray = pat.toCharArray();
    Arrays.sort(patTempArray);
    
    String patResult = new String(patTempArray);
    
    for(int j = 0; j <= (txt.length() - pat.length()); j++)
    {
            
        String check = txt.substring(j, j + (pat.length()));
        char[] tempSub = check.toCharArray();
        Arrays.sort(tempSub);
        String checkResult = new String(tempSub);
        
        if(checkResult.equals(patResult))
        {
            return true;
        }
    }
    
    return false;
  }
added 217 characters in body
Source Link

I am working on a problem on LeetCode. The link to it is https://leetcode.com/problems/permutation-in-string/description/?envType=problem-list-v2&envId=string

Considering only lowercase characters from 'a' to 'z', I have written some Java code to check if a permutation of a given pattern is a substring of a given string. I have used the sliding window technique to determine the output. Could someone please let me know if my approach is optimal in terms of time complexity and space complexity? Any help with determining the time complexity would be appreciated as well.

public static boolean permSubstring(String txt, String pat)
  {
    char[] patTempArray = pat.toCharArray();
    Arrays.sort(patTempArray);
    
    String patResult = new String(patTempArray);
    
    for(int j = 0; j <= (txt.length() - pat.length()); j++)
    {
            
        String check = txt.substring(j, j + (pat.length()));
        char[] tempSub = check.toCharArray();
        Arrays.sort(tempSub);
        String checkResult = new String(tempSub);
        
        if(checkResult.equals(patResult))
        {
            return true;
        }
    }
    
    return false;
  }

I have written some Java code to check if a permutation of a given pattern is a substring of a given string. I have used the sliding window technique to determine the output. Could someone please let me know if my approach is optimal in terms of time complexity and space complexity? Any help with determining the time complexity would be appreciated as well.

public static boolean permSubstring(String txt, String pat)
  {
    char[] patTempArray = pat.toCharArray();
    Arrays.sort(patTempArray);
    
    String patResult = new String(patTempArray);
    
    for(int j = 0; j <= (txt.length() - pat.length()); j++)
    {
            
        String check = txt.substring(j, j + (pat.length()));
        char[] tempSub = check.toCharArray();
        Arrays.sort(tempSub);
        String checkResult = new String(tempSub);
        
        if(checkResult.equals(patResult))
        {
            return true;
        }
    }
    
    return false;
  }

I am working on a problem on LeetCode. The link to it is https://leetcode.com/problems/permutation-in-string/description/?envType=problem-list-v2&envId=string

Considering only lowercase characters from 'a' to 'z', I have written some Java code to check if a permutation of a given pattern is a substring of a given string. I have used the sliding window technique to determine the output. Could someone please let me know if my approach is optimal in terms of time complexity and space complexity? Any help with determining the time complexity would be appreciated as well.

public static boolean permSubstring(String txt, String pat)
  {
    char[] patTempArray = pat.toCharArray();
    Arrays.sort(patTempArray);
    
    String patResult = new String(patTempArray);
    
    for(int j = 0; j <= (txt.length() - pat.length()); j++)
    {
            
        String check = txt.substring(j, j + (pat.length()));
        char[] tempSub = check.toCharArray();
        Arrays.sort(tempSub);
        String checkResult = new String(tempSub);
        
        if(checkResult.equals(patResult))
        {
            return true;
        }
    }
    
    return false;
  }
Rollback to Revision 3
Source Link
TorbenPutkonen
  • 9.3k
  • 21
  • 29
Loading
Became Hot Network Question
added 943 characters in body
Source Link
Loading
added 2 characters in body
Source Link
Loading
edited tags
Link
Loading
Source Link
Loading