Checking strings against each other (Anagrams)

后端 未结 23 1672
忘了有多久
忘了有多久 2020-11-30 10:55

The assignment is to write a program that accepts two groups of words from the user and then prints a \"True\" statement if the two are anagrams (or at least if all the lett

相关标签:
23条回答
  • 2020-11-30 11:11

    Java Code for Anagram

    static void anagram(String s1,String s2){
        if(s1.length()!=s2.length()){
            System.out.println("not anagram");
            return;
        }
        else{
            int []arr=new int[256];
    
            int size=s1.length();
            for(int i=0;i<size;i++){
                arr[s1.charAt(i)]++;
                arr[s2.charAt(i)]--;
            }
            for(int i=0;i<256;i++){
                if(arr[i]!=0){
                    System.out.println("not anagram");
                    return;
                    }
            }
            System.out.println("anagram");
        }
        
    }
    
    0 讨论(0)
  • 2020-11-30 11:12

    Not sure if it was proposed up there, but I went with:

    def is_anagram(a, b):
        return sorted(a.lower()) == sorted(b.lower())
    
    0 讨论(0)
  • 2020-11-30 11:13

    To check if two strings are anagrams of each other using dictionaries: Note : Even Number, special characters can be used as an input

    def anagram(s):
        string_list = []
        for ch in s.lower():
            string_list.append(ch)
    
        string_dict = {}
        for ch in string_list:
            if ch not in string_dict:
                string_dict[ch] = 1
            else:
                string_dict[ch] = string_dict[ch] + 1
    
        return string_dict
    
    
    
    s1 = "master"
    s2 = "stream"
    
    a = anagram(s1)
    b = anagram(s2)
    
    if a == b:
        print "Anagram"
    else:
        print "Not Anagram"
    
    0 讨论(0)
  • 2020-11-30 11:14

    Here's one typical assignment solution:

    def anagram(s1, s2):
    """ (str, str) -> bool
    
    Return True if s1 and s2 are anagrams
    
    >>> anagram(s1, s2)
    True
    """
        s1 = s1.replace(" ", "")
        s2 = s2.replace(" ", "")
    
        s1_new = list(s1)
        s2_new = list(s2)
    
        if len(s1_new) == len(s2_new):
            dupe_found = True
            while dupe_found:
                dupe_found = False
                for i in s1_new:
                    for j in s2_new:
                        if i == j:
                            s1_new.remove(i)
                            s2_new.remove(j)
                            dupe_found = True
                            break
                    break
        return s1_new == s2_new
    
    0 讨论(0)
  • 2020-11-30 11:15

    This worked for me

    str1="abcd"
    str2="bcad"
    word1=[]
    word2=[]
    for x in range(len(str1)):
        word1.append(str1[x])
    for x in range(len(str2)):
        word2.append(str2[x])
    if(len(word1)==len(word2)):
        for letter in word1:
            if letter in word2:
                word2.remove(letter)
    
    if len(word2)==0:
        print "anagram"
    else:
        print "not anagram"
    
    0 讨论(0)
  • 2020-11-30 11:18
    def is_anagram(w1, w2):
        w1, w2 = list(w1.upper()), list(w2.upper())
        w2.sort()
        w1.sort()
        return w1 == w2
    
    0 讨论(0)
提交回复
热议问题