Simple
Given two strings:
ransomNote
andmagazine
, determineransomNote
whether they canmagazine
be composed of the characters inside.Return if possible
true
; otherwise returnfalse
.
magazine
Each character in can onlyransomNote
be used once in .Example 1:
Input: ransomNote = "a", magazine = "b" Output: falseExample 2:
Input: ransomNote = "aa", magazine = "ab" Output: falseExample 3:
Input: ransomNote = "aa", magazine = "aab" Output: truehint:
1 <= ransomNote.length, magazine.length <= 105
ransomNote
andmagazine
consists of lowercase English lettersbool 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; }
Simple
Given two strings
s
andt
, determine whether they are isomorphic.If
s
the characters in can be replaced according to a certain mapping relationshipt
, 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: trueExample 2:
Input: s = "foo", t = "bar" Output: falseExample 3:
Input: s = "paper", t = "title" Output: truehint:
1 <= s.length <= 5 * 104
t.length == s.length
s
andt
consists of any valid ASCII charactersbool 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; }
Simple
Given a pattern
pattern
and a strings
, determines
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: trueExample 2:
Input: pattern = "abba", s = "dog cat cat fish" Output: falseExample 3:
Input: pattern = "aaaa", s = "dog cat cat dog" Output: falsehint:
1 <= pattern.length <= 300
pattern
Contains only lowercase English letters1 <= s.length <= 3000
s
Contains only lowercase English letters and' '
s
Does not contain any leading or trailing pairs of spacess
Each word in is separated by a single spacebool 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; }
Simple
Given two strings
s
andt
, write a function to determinet
whether iss
an anagram of .Note: If each character in
s
andt
occurs the same number of times, thens
and are saidt
to be anagrams of each other.Example 1:
Input: s = "anagram", t = "nagaram" Output: trueExample 2:
Input: s = "rat", t = "car" Output: falsehint:
1 <= s.length, t.length <= 5 * 104
s
andt
contain only lowercase lettersbool 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; }
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; }