[leetcode]Valid Anagram解题报告 C语言

【题目】

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

For example,

s = “anagram”, t = “nagaram”, return true.

s = “rat”, t = “car”, return false.

Note:

You may assume the string contains only lowercase alphabets.

【题目分析】

这道题就是判断s,t两个字符串里所含的每一种字母个数是否一样,一样,则返回true,不一样则返回false。采用的办法是用一个int check_s[26]来记录每个字母的个数。一次遍历,对于s中出现的字母,对应的check_s数组中元素加1,对于t中出现的字母,对应的check_s数组中元素减一,最后,判断check_s中所有元素是否全部是0,有非0,返回false,否则,返回true.

【具体代码如下】

bool isAnagram(char* s, char* t) {
 int check_s[26]={0};
 int i;
 int indexs;
 int indext;
 for(i=0;(s[i]!=‘\0‘)||(t[i]!=‘\0‘);i++)
 {
    indexs=s[i]-‘a‘;
    check_s[indexs]++;
    indext=t[i]-‘a‘;
    check_s[indext]--;
 }
 for(i=25;i>=0;i--)
 {
    if(check_s[i]!=0)
    return false;
 }
 return true;

}

【个人总结】

一开始,我是用了两个数组分别记录s,t中所含字母的情况。后来进一步思考,减小了空间使用。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2025-01-09 08:11:17

[leetcode]Valid Anagram解题报告 C语言的相关文章

[leetcode]Valid Sudoku 解题报告 C 语言

[题目] Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. Note: A valid Sudoku board (partially filled) is not necessarily solvable.

LeetCode: Valid Parentheses 解题报告

Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" a

LeetCode: Valid Sudoku 解题报告

Valid SudokuDetermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note:A valid Sudoku board

LeetCode: Valid Palindrome 解题报告

Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you

LeetCode: Valid Number 解题报告

Valid NumberValidate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambi

[leetcode]Isomorphic Strings 解题报告 C语言

[题目] Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of charact

[leetcode]Ugly Number 解题报告 C语言

[题目] Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1

[leetcode]Count Primes 解题报告 C语言

[题目] Count the number of prime numbers less than a non-negative number, n. Credits: Special thanks to @mithmatt for adding this problem and creating all test cases. [题目分析] 这道题常用的判断一个数是否为质数是行不通的,根据hint,采用Sieve of Eratosthenes算法实现,具体关于该算法详见https://en.w

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i