< Summary

Information
Class: LeetCode.Algorithms.BuddyStrings.BuddyStringsCounting
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\BuddyStrings\BuddyStringsCounting.cs
Line coverage
89%
Covered lines: 34
Uncovered lines: 4
Coverable lines: 38
Total lines: 79
Line coverage: 89.4%
Branch coverage
90%
Covered branches: 20
Total branches: 22
Branch coverage: 90.9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
BuddyStrings(...)90.9%232289.47%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\BuddyStrings\BuddyStringsCounting.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.BuddyStrings;
 13
 14/// <inheritdoc />
 15public class BuddyStringsCounting : IBuddyStrings
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="s"></param>
 22    /// <param name="goal"></param>
 23    /// <returns></returns>
 24    public bool BuddyStrings(string s, string goal)
 525    {
 526        if (s.Length != goal.Length || s.Length < 2)
 127        {
 128            return false;
 29        }
 30
 431        if (s == goal)
 232        {
 233            var duplicates = new bool[26];
 34
 1735            foreach (var index in s.Select(c => c - 'a'))
 436            {
 437                if (duplicates[index])
 138                {
 139                    return true;
 40                }
 41
 342                duplicates[index] = true;
 343            }
 44
 145            return false;
 46        }
 47
 248        var firstIndex = -1;
 249        var secondIndex = -1;
 50
 1851        for (var i = 0; i < s.Length; i++)
 752        {
 753            if (s[i] == goal[i])
 354            {
 355                continue;
 56            }
 57
 458            if (firstIndex == -1)
 259            {
 260                firstIndex = i;
 261            }
 262            else if (secondIndex == -1)
 263            {
 264                secondIndex = i;
 265            }
 66            else
 067            {
 068                return false;
 69            }
 470        }
 71
 272        if (secondIndex == -1)
 073        {
 074            return false;
 75        }
 76
 277        return s[firstIndex] == goal[secondIndex] && s[secondIndex] == goal[firstIndex];
 578    }
 79}