首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >java-字符串处理

java-字符串处理

作者头像
luxuantao
发布2021-02-24 11:23:55
发布2021-02-24 11:23:55
44500
代码可运行
举报
文章被收录于专栏:Fdu弟中弟Fdu弟中弟
运行总次数:0
代码可运行

java的字符串处理,有涉及到HushMap和其他一些零散知识点的应用,作为初学者,这题就作为范例供来日所需。

原题链接:Java Anagrams

Two strings, and , are called anagrams if they contain all the same characters in the same frequencies. For example, the anagrams of CAT are CAT, ACT, TAC, TCA, ATC, and CTA.

Complete the function in the editor. If and are case-insensitive anagrams, print “Anagrams”; otherwise, print “Not Anagrams” instead.

Input Format

The first line contains a string denoting . The second line contains a string denoting .

Constraints

  • Strings and consist of English alphabetic characters.
  • The comparison should NOT be case sensitive.

Output Format

Print “Anagrams” if and are case-insensitive anagrams of each other; otherwise, print “Not Anagrams” instead.

Sample Input 0

代码语言:javascript
代码运行次数:0
运行
复制
anagram
margana

Sample Output 0

代码语言:javascript
代码运行次数:0
运行
复制
Anagrams

Explanation 0

Character

Frequency: anagram

Frequency: margana

A or a

3

3

G or g

1

1

N or n

1

1

M or m

1

1

R or r

1

1

The two strings contain all the same letters in the same frequencies, so we print “Anagrams”.

Sample Input 1

代码语言:javascript
代码运行次数:0
运行
复制
anagramm
marganaa

Sample Output 1

代码语言:javascript
代码运行次数:0
运行
复制
Not Anagrams

Explanation 1

Character

Frequency: anagramm

Frequency: marganaa

A or a

3

4

G or g

1

1

N or n

1

1

M or m

2

1

R or r

1

1

The two strings don’t contain the same number of a‘s and m‘s, so we print “Not Anagrams”.

Sample Input 2

代码语言:javascript
代码运行次数:0
运行
复制
Hello
hello

Sample Output 2

代码语言:javascript
代码运行次数:0
运行
复制
Anagrams

Explanation 2

Character

Frequency: Hello

Frequency: hello

E or e

1

1

H or h

1

1

L or l

2

2

O or o

1

1

The two strings contain all the same letters in the same frequencies, so we print “Anagrams”.

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
import java.io.*;
import java.util.*;
public class Solution {
  static boolean isAnagram(String a, String b) {
        if (a.length()!=b.length())
            return false;
        Map<Character, Integer> map = new HashMap<>();
        a=a.toLowerCase();
        b=b.toLowerCase();
        for(int i=0;i<a.length();i++) {
            char ch=a.charAt(i);
            if(!map.containsKey(ch)) {
                map.put(ch,1);
            } 
            else {
                Integer frequency = map.get(ch);
                map.put(ch,++frequency);
            }
        }
        for(int i=0;i<b.length();i++) {
            char ch=b.charAt(i);
            if(!map.containsKey(ch)) {
                return false;
            } 
            Integer frequency = map.get(ch);
            if(frequency==0) {
                return false;
            }
            else {
                map.put(ch,--frequency);
            }
        }
        return true;
  }
  public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String a = scan.next();
        String b = scan.next();
        scan.close();
        boolean ret = isAnagram(a, b);
        System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
  }
}

方法二:

代码语言:javascript
代码运行次数:0
运行
复制
import java.util.*;

public class Solution {

    static boolean isAnagram(String s1, String s2) {
        if (s1.length() != s2.length()) {
            return false;
        }
        
        char[] a = s1.toLowerCase().toCharArray();
        char[] b = s2.toLowerCase().toCharArray();
        boolean anagram = true;
        Arrays.sort(a);
        Arrays.sort(b);
        
        for(int i = 0; i < a.length; i++) {
            if(a[i] != b[i]) {
                anagram = false;
            }
        }
        
        return anagram;
    }
  
    public static void main(String[] args) {
    
        Scanner scan = new Scanner(System.in);
        String a = scan.next();
        String b = scan.next();
        scan.close();
        boolean ret = isAnagram(a, b);
        System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-09-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档