Untitled

 avatar
user_5481415
plain_text
2 years ago
2.7 kB
4
Indexable
int calculateSameAlphabetCount(String str1, String str2) {
  // Convert the strings to lowercase for case-insensitive comparison
  String lowercaseStr1 = str1.toLowerCase();
  String lowercaseStr2 = str2.toLowerCase();

  // Create a set to store unique alphabets from str1
  Set<String> uniqueAlphabets = Set<String>.from(lowercaseStr1.split(''));

  // Initialize the count of same alphabets to 0
  int sameAlphabetCount = 0;

  // Iterate over each alphabet in uniqueAlphabets set
  for (String alphabet in uniqueAlphabets) {
    // Check if the alphabet exists in both strings
    if (lowercaseStr2.contains(alphabet)) {
      // Increment the sameAlphabetCount
      sameAlphabetCount++;
    }
  }

  return sameAlphabetCount;
}

void main() {
  String str = 'sweety rawat';

  // Count the occurrence of each alphabet in the string
  Map<String, int> alphabetCounts = countAlphabets(str);

  // Output the count of each alphabet
  for (String alphabet in alphabetCounts.keys) {
    int count = alphabetCounts[alphabet]!;
    print('Count of $alphabet: $count');
  }

  String str1 = 'sweety rawat';
  String str2 = 'sweetie';

  int result = calculateSameAlphabetCount(str1, str2);

  print('Number of same alphabets: $result');
}



Map<String, int> countAlphabets(String str) {
  // Convert the string to lowercase for case-insensitive comparison
  String lowercaseStr = str.toLowerCase();

  // Create a map to store alphabet counts
  Map<String, int> alphabetCounts = {};

  // Iterate over each character in the string
  for (int i = 0; i < lowercaseStr.length; i++) {
    String character = lowercaseStr[i];

    // Check if the character is an alphabet
    if (character.contains(RegExp(r'[a-z]'))) {
      // If the alphabet is already present in the map, increment its count
      if (alphabetCounts.containsKey(character)) {
        alphabetCounts[character] = (alphabetCounts[character]! + 1);
      } else {
        // If the alphabet is not present in the map, add it with a count of 1
        alphabetCounts[character] = 1;
      }
    }
  }

  return alphabetCounts;
}


// void main() {
//   String name = "SweetyRawatchosen";
//
//   int countE = 0;
//   int countA = 0;
//
//   print(getString(name));
//   // name.splitMapJoin(RegExp(""),)
//   // for (int i = 0; i < name.length; i++) {
//   //   print('index of string $i: ${name[i]}');
//   //   if(name[i]=="e"){
//   //     countE++;
//   //   }
//   // }
// }
//
// String getString(String string) {
//   String returnString = '';
//   string.split(' ').forEach((word) => returnString += '\n $word');
//   return string.splitMapJoin((new RegExp(r'chosen')),
//       onMatch: (m) => ' ${m.group(0)} ', onNonMatch: (n) => '[other word]');
// }
Editor is loading...