ThinkVireo
  • Home (current)
  • About
  • TCS IPA/DCA(new)
    TCS DCA TCS IPA Python TCS IPA JAVA
  • FAQs
    TCS 2025
  • Contact
  • India
  • Leetcode

Anagram

    Question: Given two strings s and t, determine whether t is an anagram of s.
    Write a Java function with the following signature to solve the problem:
    The function should take two strings s and t as input and return a boolean value indicating whether t is an anagram of s.
    Note: An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

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

    1. JAVA Code:
      
          public static boolean isAnagram(String s, String t) {
              if (s.length() != t.length()) {
                  return false;
              }
          
              int[] count = new int[26];
          
              for (int i = 0; i < s.length(); i++) {
                  count[s.charAt(i) - 'a']++;
                  count[t.charAt(i) - 'a']--;
              }
          
              for (int i = 0; i < count.length; i++) {
                  if (count[i] != 0) {
                      return false;
                  }
              }
          
              return true;
          }
          
                  


    Shubham Shrivas


ThinkVireo

ThinkVireo

TCS IPA Leetcode Python Java Partners Careers
FAQ Contact About Support Blog Services
Privacy & Policy

©ThinkVireo 2025 . All rights reserved.