Classic interview question: Hash table

383. Ransom letter

Simple

Given two strings: ransomNote and  magazine , determine  ransomNote whether they can  magazine be composed of the characters inside.

Return if possible  true ; otherwise return  false .

magazine Each character in can only  ransomNote be used once in .

Example 1:

Input: ransomNote = "a", magazine = "b"
 Output: false

Example 2:

Input: ransomNote = "aa", magazine = "ab"
 Output: false

Example 3:

Input: ransomNote = "aa", magazine = "aab"
 Output: true

hint:

  • 1 <= ransomNote.length, magazine.length <= 105
  • ransomNote and  magazine consists of lowercase English letters
bool canConstruct(char* ransomNote, char* magazine) {
    int rr=strlen(ransomNote);
    int mm=strlen(magazine);
    int count=0;
    for(int i=0;i<rr;i++){
        for(int j=0;j<mm;j++){
            if(ransomNote[i]==magazine[j]){
               count++;
               magazine[j]=' ';
               break;
            }
        }
    }
    if(count==rr){
        return true;
    }
     return false;
}

205. Isomorphic strings

Simple

Given two strings  s and  t , determine whether they are isomorphic.

If  s the characters in can be replaced according to a certain mapping relationship  t , then the two strings are isomorphic.

Each occurrence of a character should be mapped to another character without changing the order of the characters. Different characters cannot be mapped to the same character, the same character can only be mapped to the same character, and characters can be mapped to themselves.

Example 1:

Input: s = "egg", t = "add"
 Output: true

Example 2:

Input: s = "foo", t = "bar"
 Output: false

Example 3:

Input: s = "paper", t = "title"
 Output: true

hint:

  • 1 <= s.length <= 5 * 104
  • t.length == s.length
  • s and  t consists of any valid ASCII characters
bool isIsomorphic(char* s, char* t) {
    int a[200],f[200]={0};
    for(int i=0;i<200;i++){
        a[i]=-1;
    }
    int ss=strlen(s);
    int tt=strlen(t);
    if(ss!=tt){
        return false;
    }
    for(int i=0;i<ss;i++){
        int p=s[i]-' ';
        if(a[p]==-1&&f[t[i]-' ']==0){
            a[p]=t[i]-' ';
            f[(t[i]-' ')]=1;
         }else if(a[p]==-1&&f[t[i]-' ']!=0){
                  return false;
         }else{
            if(a[p]!=t[i]-' '){
                   return false;
            }
       }

    }

    return true;
}

290. Word patterns

Simple

Given a pattern  pattern and a string  s , determine  s whether it follows the same pattern.

Follow  here  refers to a complete match. For example,  there is a two-way connection between pattern each letter in and each non-empty word in the string  .s 

Example 1:

Input: pattern = "abba", s = "dog cat cat dog"
 Output: true

Example 2:

Input: pattern = "abba", s = "dog cat cat fish"
 Output: false

Example 3:

Input: pattern = "aaaa", s = "dog cat cat dog"
 Output: false

hint:

  • 1 <= pattern.length <= 300
  • pattern Contains only lowercase English letters
  • 1 <= s.length <= 3000
  • s Contains only lowercase English letters and ' '
  • s Does not contain  any leading or trailing pairs of spaces
  • s Each word in is  separated by a single space 
bool wordPattern(char* pattern, char* s) {
    char *hashset[26];
    for(int i=0;i<26;i++){
        hashset[i]=NULL;
    }
    int pp=strlen(pattern);
    const char ff[2]=" ";
    char *token=NULL;
    for(int i=0;i<pp;i++){
      
        if(token == NULL){
            token = strtok(s, ff);
        }
        else{
             token = strtok(NULL, ff);
             if(token == NULL) {
                 return false;
             }
        }
        if(hashset[pattern[i]-'a']==NULL){
            
                 hashset[pattern[i]-'a']=token;
       }else{
           if(strcmp(hashset[pattern[i]-'a'],token)!=0){
                   return false;
            }
        }
    }
    token = strtok(NULL, ff);
    if(token != NULL)
    {
        return false;
    }
    for(int i =0; i < 26; i++)
    {
        if(hashset[i]!= NULL)
        {
            for(int j = i+ 1; j < 26; j++)
            {
                if(hashset[j]!= NULL)
                {
                    if(strcmp(hashset[i], hashset[j]) == 0)
                    {
                        return false;
                    }
                }
            }
        }
    }
    return true;
}

242. Valid anagrams

Simple

Given two strings  s and  t , write a function to determine  t whether is  s an anagram of .

Note: If  each character in s and  t occurs the same number of times, then  s and  are said t to be anagrams of each other.

Example 1:

Input:  s = "anagram", t = "nagaram"
 Output: true

Example 2:

Input:  s = "rat", t = "car"
 Output: false

hint:

  • 1 <= s.length, t.length <= 5 * 104
  • s and  t contain only lowercase letters
bool isAnagram(char* s, char* t) {
    int snum[26]={0};
    int tnum[26]={0};
    int ss=strlen(s);
    int tt=strlen(t);
    for(int i=0;i<ss;i++){
        snum[s[i]-'a']++;
    }
    for(int i=0;i<tt;i++){
        tnum[t[i]-'a']++;
    }
    for(int i=0;i<26;i++){
        if(snum[i]!=tnum[i]){
            return false;
        }
    }
    return true;
}

1. The sum of two numbers

Simple

Given an integer array  nums and an integer target value  , please find   the  twotarget integers in the array whose sum  is the target value   and return their array subscripts.target

You can assume that each input will correspond to only one answer. However, the same element in the array cannot appear repeatedly in the answer.

You can return answers in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
 Output: [0,1]
 Explanation: Because nums[0] + nums[1] == 9, [0, 1] is returned.

Example 2:

Input: nums = [3,2,4], target = 6
 Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6
 Output: [0,1]

hint:

  • 2 <= nums.length <= 104
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • There will only be one valid answer
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
    int *a=(int*)malloc(sizeof(int)*2);
    for(int i=0;i<numsSize;i++){
        for(int j=i+1;j<numsSize;j++){
            if(nums[i]+nums[j]==target){
               a[0]=i;
               a[1]=j;
               (*returnSize)=2;
                break;
            }
        }
    }
     return a;
}

Supongo que te gusta

Origin blog.csdn.net/weixin_63357306/article/details/135152536
Recomendado
Clasificación